jamiewilson/form-to-google-sheets

intialSetup not called

ajmas opened this issue · 0 comments

ajmas commented

When running I found that intialSetup() was not being called, maybe due to a missed step by me? For this reason I removed it and simply assigned the spreadsheet id during the POST call:

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

  try {
     // line below changed:
    const doc = SpreadsheetApp.openById(SpreadsheetApp.getActiveSpreadsheet().getId());
    const sheet = doc.getSheetByName(sheetName);

    const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    const nextRow = sheet.getLastRow() + 1;

    if (event) {
      const newRow = headers.map(function(header) {
        return header === 'timestamp' ? new Date() : event.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 (error) {
    return ContentService
      .createTextOutput(JSON.stringify({ 'result': 'error', 'error': error }))
      .setMimeType(ContentService.MimeType.JSON)
  }
  finally {
    lock.releaseLock()
  }
}