0% found this document useful (0 votes)
27 views1 page

HTML To Google Sheet

This document contains a Google Apps Script that sets up a Google Sheets integration. It includes functions for initial setup and handling POST requests to add new rows to a specified sheet, capturing data from incoming parameters. The script ensures data integrity with locking mechanisms and returns success or error responses in JSON format.

Uploaded by

other4482
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views1 page

HTML To Google Sheet

This document contains a Google Apps Script that sets up a Google Sheets integration. It includes functions for initial setup and handling POST requests to add new rows to a specified sheet, capturing data from incoming parameters. The script ensures data integrity with locking mechanisms and returns success or error responses in JSON format.

Uploaded by

other4482
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

const sheetName = 'Sheet1'

const scriptProp = PropertiesService.getScriptProperties()

function intialSetup () {
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()
}
}

You might also like