> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kameleoon.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Send survey responses to Google Sheets

> Configure a Google Apps Script and Widget Studio survey settings to automatically send survey responses to a Google Sheet via HTTP POST.

Apps Script can receive each survey response from the Widget Studio and append it as a row in a Google Sheet.

> [!NOTE]
>
> If you want to collect responses from your widget in Kameleoon and export them to CSV, refer to the [Survey data export](https://docs.kameleoon.com/user-manual/assets/widgets/create-a-widget-with-the-widget-studio/create-csat-nps-and-surveys/survey-data-export.md) article.

## Configure your Google Sheet

1. Go to [Google Sheets](https://sheets.google.com/) and create a new spreadsheet.
2. Name the tab **Sheet1** (which is the **default**).
3. In the first row, define the columns as follows:

   ![Google Sheet with Date in cell A1 and placeholder survey element names in cells B1 and C1](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/assets/widgets/create-a-widget-with-the-widget-studio/create-csat-nps-and-surveys/send-survey-responses-to-google-sheets/google-sheet-row1.png)

   **Details:**

   * **Column A:** Enter **Date**. The script fills this cell with the date and time of each submission.
   * **From column B onward:** Enter the name of each survey or form element added to your widget.

   You can find an element's name in the elements tree in the **Content** tab of the **Design** section.

   ![Widget Studio Content tab with the NPS Screen and an Elements list showing Popup, Close, Heading, Scale, Long Answer, Submit button, and Background](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/assets/widgets/create-a-widget-with-the-widget-studio/create-csat-nps-and-surveys/send-survey-responses-to-google-sheets/elements-tree.png)

   When you select an element (for example, a scale), its name appears at the top of the editing sidebar.

   ![Scale element settings with Scale type set to Emoji, an Edit name tooltip, and an emoji feedback widget on the canvas](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/assets/widgets/create-a-widget-with-the-widget-studio/create-csat-nps-and-surveys/send-survey-responses-to-google-sheets/edit-name-1536x842.png)

   Consider this widget as an example:

   ![Widget Studio Content tab and a We value your feedback survey preview with emoji scale, comment field, and Submit button](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/assets/widgets/create-a-widget-with-the-widget-studio/create-csat-nps-and-surveys/send-survey-responses-to-google-sheets/example-widget-1536x1018.png)

   This widget has two survey elements, so enter the following in the first row of the Google Sheet:

   * **Column A:** Date
   * **Column B:** Scale
   * **Column C:** Long Answer

Add the Google Apps Script to your spreadsheet:

4. Click **Extensions** > **Apps Script**. A new Google Script opens. Rename it. For example, use "form data."

   ![Blank Google Sheets spreadsheet named Untitled spreadsheet, with the pointer on the Extensions menu](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/assets/widgets/create-csat-nps-and-surveys/send-survey-responses-to-google-sheets/0.png)

5. Replace all code with the following:

   ```javascript theme={null}
   const sheetName = 'Sheet1'
   const scriptProp = PropertiesService.getScriptProperties()

   function initialSetup () {
     const activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
     scriptProp.setProperty('key', activeSpreadsheet.getId())
   }

   function doPost (e) {
     const lock = LockService.getScriptLock()
     lock.tryLock(10000)

     try {
       const doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
       const sheet = doc.getSheetByName(sheetName)

       const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
       const nextRow = sheet.getLastRow() + 1
       const newRow = headers.map(function(header) {
         return header === 'Date' ? new Date() : e.parameter[header]
       })

       sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])

       return ContentService
         .createTextOutput(JSON.stringify({ 'result': 'success', 'row': nextRow }))
         .setMimeType(ContentService.MimeType.JSON)
     }
     catch (e) {
       return ContentService
         .createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
         .setMimeType(ContentService.MimeType.JSON)
     }
     finally {
       lock.releaseLock()
     }
   }
   ```

   > [!NOTE]
   >
   > If you can't name your tab "Sheet1," change the value of `sheetName` in the first line of the script to your tab's actual name.

   ![Google Apps Script editor for a project named My form data, showing Code.gs with the initialSetup and doPost functions](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/assets/widgets/create-csat-nps-and-surveys/send-survey-responses-to-google-sheets/1.png)

6. Click **Save project to Drive** (to the left of **Run**).

7. In the dropdown to the right of **Debug**, select **`initialSetup`** (usually selected by default).

8. Click **Run**.

   You might see a pop-in asking for permissions. Click **Review permission** and continue to the next screen.

   Because Google hasn't reviewed the script, a warning message may appear. Click **Go to form data (unsafe)** to grant the permissions that let the script update your spreadsheet.

   ![Google warning page Google hasn't verified this app, with an arrow pointing to the Go to Mailing List (unsafe) link](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/assets/widgets/create-csat-nps-and-surveys/send-survey-responses-to-google-sheets/2.png)

   After you grant the permissions, the console shows Execution completed.

   ![Apps Script Execution log showing Execution started and Execution completed notices](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/assets/widgets/create-csat-nps-and-surveys/send-survey-responses-to-google-sheets/3.png)

9. In the left menu, click **Triggers** to add a trigger for the script.

   ![Apps Script Triggers page with a trigger for the doPost function from spreadsheet on form submit](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/assets/widgets/create-csat-nps-and-surveys/send-survey-responses-to-google-sheets/4.png)

10. Click **Add trigger**. In the window that appears, select the following options:

    * **Choose which function to run:** `doPost`
    * **Choose which deployment should run:** `Head`
    * **Select event source:** `From spreadsheet`
    * **Select event type:** `On form submit`

    ![Google Apps Script 'Edit Trigger for My form data' dialog with doPost, Head, From spreadsheet, On form submit, and Save](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/assets/widgets/create-csat-nps-and-surveys/send-survey-responses-to-google-sheets/5.png)

11. Click **Save**.

12. Click **Deploy** > **New deployment**.

13. Click the **cog** icon next to **Select type** > **Web app**. In the form that appears, select the following options:

    * **Description:** Use a descriptive name, such as "form data."
    * **Web app > Execute as:** `Me`
    * **Web app > Who has access:** `Anyone`

14. Click **Deploy**.

    > [!WARNING]
    >
    > **Who has access: `Anyone`** is required for the widget to send responses to your spreadsheet.

    ![New deployment dialog showing the deployment ID and Web app URL, with the Copy button under the URL highlighted](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/assets/widgets/create-csat-nps-and-surveys/send-survey-responses-to-google-sheets/7.png)

15. Copy the URL.

## Configure survey settings in the Widget Studio

1. In the **Survey Settings** tab, turn on **Collect survey data**.
2. Under **When is the survey submitted?**, select **A button is clicked**, then choose the button that visitors click to submit.
3. Under **How are responses collected?**, choose **Externally (HTTP Request)**.
   * **Request name:** Name your request. For example, use "send data to Google Sheet."
   * **Method:** Select **`POST`**.
   * **Action URL:** Paste the URL generated by your Google Apps Script.
4. Save your configuration.

## Test your setup

1. **Preview** your widget.
2. Fill out the survey and **Submit**.
3. Go back to your Google Sheet.

You should now see a new row with your response.
