Google Sheets Integration
Setting up Google Sheets has two parts:
- Create a Web App URL in Google Sheets.
- Add that URL to Fluentos.
1. Generate Web App URL from Google Sheets
Section titled “1. Generate Web App URL from Google Sheets”- Open your Google Sheet.
- Go to Extensions > Apps Script.
- Replace the default code with this code:
var LOCK_WAIT_MS = 30000var 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, )}- Save the project, then click Deploy > Manage Deployments.
- Click Create Deployment.
- Click Select type > Web app.
- In Who has access, select Anyone, so Fluentos can send data to it.
- Click Deploy.
- Click Authorize access, then click Allow.
- 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/exec2. Add the Web App URL to Fluentos
Section titled “2. Add the Web App URL to Fluentos”On the left, click Integrations, click Add Integration, then:
- Enter an Integration Name, for example
Google Sheets. - In Select Integration, choose
Custom. - Turn on Enabled.
- In Method, choose
POST. - In Endpoint URL, paste the Web app URL you copied from Google Sheets.
- Click Add New Header. Choose
Content-Typeas Header Name and enterapplication/jsonas Value. - Click Add Parameter. Enter
submissionIdas 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. - In Notification Recipients, choose the team members who get an email when submissions can’t be delivered.
- Click Add.