Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script not working #36

Open
rhanb opened this issue Jul 14, 2020 · 1 comment
Open

Script not working #36

rhanb opened this issue Jul 14, 2020 · 1 comment

Comments

@rhanb
Copy link

rhanb commented Jul 14, 2020

Hi,

I tried to use your script but it didn't work straight away.

The issue was coming from this line

sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])

I think it's because the nextRow didn't exist yet. On each call an empty error object was returned. By removing this line the code was working (without inserting the data obviously).

So I changed the script a bit, instead of using setValues I'm using appendRow.

Here is the final version of the doPost function I'm using:

function doPost (e) {
  var lock = LockService.getScriptLock()
  lock.tryLock(10000)

  try {
    var doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
    var sheet = doc.getSheetByName(sheetName)

    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
    
    var newRow = headers.map(function(header) {
      return header === 'timestamp' ? new Date() : e.parameter[header]
    })
    
    sheet.appendRow(newRow)

    return ContentService
    .createTextOutput(JSON.stringify({ 'result': 'success', 'row': newRow}))
      .setMimeType(ContentService.MimeType.JSON) 
  }

  catch (e) {
    return ContentService
      .createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
      .setMimeType(ContentService.MimeType.JSON)
  }

  finally {
    lock.releaseLock()
  }
}

Debugging Google Scripts is a real pane so thanks for the amazing work you've done on the script 👍

@n4j1Br4ch1D
Copy link

n4j1Br4ch1D commented Dec 5, 2020

this is why data dont show on your sheet .hi is this related?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants