> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://ajuda.greatsoftwares.com.br/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# How to INTEGRATE VIA WEBHOOK with Google Sheets

| IMPORTANT: Google is limiting some features within basic Google accounts. To integrate with Google Sheets, you need a GSuite account.

1. Go to the **Google Sheets** home page and create a new blank spreadsheet:

![](https://storage.crisp.chat/users/helpdesk/website/a6ab90d8350d6000/capturarpng_ud072e.png)

2. On the first tab, enter the names of each field that will be filled in on your landing page form, one per column (as in the example below):

![](https://storage.crisp.chat/users/helpdesk/website/a6ab90d8350d6000/capturar2png_1157ghe.png)

3. After that, go to the **"Tools > Script Editor"** menu (or **"Ferramentas > Editor de Script"**, if your language is set to Portuguese).

![](https://storage.crisp.chat/users/helpdesk/website/a6ab90d8350d6000/capturar3png_1ib3q2w.png)

4. Once you access the scripts area, you will see a screen like the image below. There, give your project a name where it says **"Untitled project"**. You can call it **"API GreatPages"**.

![](https://storage.crisp.chat/users/helpdesk/website/a6ab90d8350d6000/projetopng_3m2cvs.png)

5. Next, select all the content and replace it with the code below:

```
// Link de compartilhamento do Google Sheets
var SPREADSHEET_URL = 'https://docs.google.com/spreadsheets/';

function doPost(e) {
  var values = getValues(e);

  if (SPREADSHEET_URL) {
    handleSpreadsheet(values, SPREADSHEET_URL);
  }

  return ContentService.createTextOutput('OK');
}

function getValues(e) {
  var values = JSON.parse(e.postData.contents);
  var answers = values["answers"];
  if (typeof answers === "object") {
    Object.keys(answers)
      .forEach(function(answerLabel) {
        var answer = answers[answerLabel];
        values[answerLabel] = answer != null ? answer.toString() : answer;
      });
    delete values["answers"];
  }
  values['received_at'] = new Date();
  return values;
}

function handleSpreadsheet(values, spreadsheetUrl) {
  var ss = SpreadsheetApp.openByUrl(spreadsheetUrl);
  var sheet = ss.getSheets()[0];
  var headerMap = getExistingKeyColMap(sheet);
  updateKeyColMap(sheet, headerMap, values);
  writeValuesWithHeaderMap(sheet, headerMap, values);
}


function getExistingKeyColMap(sheet) {
  var map = {};
  sheet.getRange('1:1').getValues()[0].forEach(function(value, colIndex){
    if (value.trim().length) {
      map[value] = colIndex + 1;
    }
  });
  return map;
}


function updateKeyColMap(sheet, headerMap, values) {
  var maxColIndex = 0;
  Object.keys(headerMap).forEach(function(key) {
    maxColIndex = Math.max(maxColIndex, headerMap[key]);
  });

  Object.keys(values).forEach(function(valueKey) {
    if (headerMap[valueKey] !== undefined) {
      return;
    }

    // The values object has a key we haven't seen before.
    maxColIndex += 1;
    sheet.getRange(1, maxColIndex).setValue(valueKey);
    headerMap[valueKey] = maxColIndex;
  });
  sheet.setFrozenRows(1);

  var headerRow = sheet.getRange("1:1");
  headerRow.setFontWeight("bold");
}

function writeValuesWithHeaderMap(sheet, headerMap, values) {
  const rowIndex = sheet.getLastRow() + 1;
  Object.keys(values).forEach(function(key) {
    const colIndex = headerMap[key];
    sheet.getRange(rowIndex, colIndex).setValue(values[key]);
  });
}

function test() {
  doPost({
    postData: {
      type: "application/json",
      contents: '{ "hello": "world" }',
    }
  })
}
```

1- On the second line, where you see -> https://docs.google.com/spreadsheets, replace it with the link to your Google spreadsheet;

||| The link to your Google spreadsheet must be open for access (public);

6. Save the script by clicking the **"Save"** icon;

![](https://storage.crisp.chat/users/helpdesk/website/a6ab90d8350d6000/salvarpng_1f9p1a8.png)

7. After that, click **"Deploy"** and, in the menu that opens, click **"New deployment"**;

![](https://storage.crisp.chat/users/helpdesk/website/a6ab90d8350d6000/implantarpng_5p99pi.png)

![](https://storage.crisp.chat/users/helpdesk/website/a6ab90d8350d6000/nova-implantacaopng_121c8ph.png)

8. On the next screen that opens, click the gear icon and then click **"Web app"**;

![](https://storage.crisp.chat/users/helpdesk/website/a6ab90d8350d6000/configuracaopng_2tids2.png)

![](https://storage.crisp.chat/users/helpdesk/website/a6ab90d8350d6000/app-da-webpng_5jle03.png)

9. After that, a space will open on the screen where you will need to add a description for the integration, select your Gmail account and, finally, select the "Anyone" option, as in the example below;

![](https://storage.crisp.chat/users/helpdesk/website/a6ab90d8350d6000/configpng_108s6gi.png)

10. After clicking **"Deploy"**, the **"Authorize access"** button will appear on the next screen, and that is where you should click;

![](https://storage.crisp.chat/users/helpdesk/website/a6ab90d8350d6000/autorizar-acessopng_106ip4p.png)

11. Next, you will be asked about some permissions. Choose a Google account and follow the process;

12. Once the permissions are complete, the integration URL will be generated. Copy it and save it so you can implement it in GreatPages;

![](https://storage.crisp.chat/users/helpdesk/website/a6ab90d8350d6000/urlpng_txs1nb.png)

13. Log in to GreatPages and open the form settings (click on the form and then click **"Edit"**;

![](https://storage.crisp.chat/users/helpdesk/website/-/a/6/a/b/a6ab90d8350d6000/editar-form_l15s0v.png =405xauto)

14. Go to **"Settings",** and in the Integration section click **"Add Integration";**

![](https://storage.crisp.chat/users/helpdesk/website/-/a/6/a/b/a6ab90d8350d6000/adicionar-integracao_1yjcio3.png)

15. Select the **“Webhook”** option;

![](https://storage.crisp.chat/users/helpdesk/website/-/a/6/a/b/a6ab90d8350d6000/webhookpadrao_u3za74.png)

16. On the next screen you will need to paste the link generated in Google Sheets into **“Webhook URL”**, and select the **“POST+JSON”** option. Finally, move on by clicking **“Continue”** *(there is no need to fill in the “token” field)*.

![](https://storage.crisp.chat/users/helpdesk/website/-/a/6/a/b/a6ab90d8350d6000/configurar-webhook_1py19sq.png)

17. On the **“Fielding mapping”** screen you will need to set up the field variables. They must be **exactly the same** as the variables you set up in the spreadsheet.

Finally, click Salvar! Your integration is ready. :)  

Still have questions? Reach out to our team on Chat! We're always happy to help. <3  