Skip to content

Google Sheets Integration

Setting up Google Sheets has two parts:

  1. Create a Web App URL in Google Sheets.
  2. Add that URL to Fluentos.

1. Generate Web App URL from Google Sheets

Section titled “1. Generate Web App URL from Google Sheets”
  1. Open your Google Sheet.
  2. Go to Extensions > Apps Script.
  3. Replace the default code with this code:
var LOCK_WAIT_MS = 30000
var ID_CACHE_SECONDS = 21600
function doPost(e) {
// Submissions can arrive at the same moment during busy periods. Without the
// lock, Google Sheets can lose a row when two appends overlap.
var lock = LockService.getScriptLock()
try {
lock.waitLock(LOCK_WAIT_MS)
} catch (error) {
// Fluentos keeps the submission and sends it again a little later
return respond({ status: 'error', message: 'busy' })
}
try {
var data = JSON.parse(e.postData.contents)
var submissionId = data.submissionId ? String(data.submissionId) : ''
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
// The same submission can be delivered twice if Google answered too slowly
// the first time. The id tells the two apart.
if (submissionId && isAlreadyStored(sheet, submissionId)) {
return respond({ status: 'success', message: 'duplicate ignored' })
}
var rowData = [new Date()]
for (var key in data) {
if (data.hasOwnProperty(key) && key !== 'submissionId') {
var value = data[key]
rowData.push(Array.isArray(value) ? value.join(',') : value)
}
}
// Always the last column, so the duplicate check knows where to look
rowData.push(submissionId)
sheet.appendRow(rowData)
if (submissionId) {
CacheService.getScriptCache().put('sid:' + submissionId, '1', ID_CACHE_SECONDS)
}
return respond({ status: 'success', message: 'Data added successfully' })
} catch (error) {
return respond({ status: 'error', message: error.toString() })
} finally {
lock.releaseLock()
}
}
function isAlreadyStored(sheet, submissionId) {
if (CacheService.getScriptCache().get('sid:' + submissionId)) {
return true
}
var lastRow = sheet.getLastRow()
var lastColumn = sheet.getLastColumn()
if (!lastRow || !lastColumn) {
return false
}
var found = sheet
.getRange(1, lastColumn, lastRow, 1)
.createTextFinder(submissionId)
.matchEntireCell(true)
.findNext()
return found !== null
}
function respond(payload) {
return ContentService.createTextOutput(JSON.stringify(payload)).setMimeType(
ContentService.MimeType.JSON,
)
}
  1. Save the project, then click Deploy > Manage Deployments.
  2. Click Create Deployment.
  3. Click Select type > Web app.
  4. In Who has access, select Anyone, so Fluentos can send data to it.
  5. Click Deploy.
  6. Click Authorize access, then click Allow.
  7. Google shows the URL of your web app. Click Copy Web app URL and keep it for the next part.

The URL looks like this:

https://script.google.com/macros/s/AKfycbxLBz3OlJH5MqeGVlCECtdeRBqKUAJUZ1qwhq1jGdXQ9jhi0rUXZKBHpjSxgNeiLbK1/exec

On the left, click Integrations, click Add Integration, then:

  1. Enter an Integration Name, for example Google Sheets.
  2. In Select Integration, choose Custom.
  3. Turn on Enabled.
  4. In Method, choose POST.
  5. In Endpoint URL, paste the Web app URL you copied from Google Sheets.
  6. Click Add New Header. Choose Content-Type as Header Name and enter application/json as Value.
  7. Click Add Parameter. Enter submissionId as Parameter Name and {{submissionId}} as Value. The script uses it to recognize a submission it has already stored, so a slow answer from Google never creates a duplicate row.
  8. In Notification Recipients, choose the team members who get an email when submissions can’t be delivered.
  9. Click Add.