0
votes

I'm appending a row in Google sheet from a simple html form ussing fetch/doPost and like to process the new row by script function.

The code is from GitHub (jamiewilson/form-to-google-sheets)

html part:
fetch(scriptURL, {method: 'POST', body: new FormData(form)})
.then(response => showSuc())
.catch(error => alert('Error! ' + error.message))

GScript part:
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 nextRow = sheet.getLastRow() + 1
    var newRow = headers.map(function(header) {
      return header === 'timestamp' ? new Date() : e.parameter[header]
    })
    sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])
    // Browser.msgBox("posted");
    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()
  }
// Browser.msgBox("posted");
}

Because adding data this way doesn't triger onEdit or onChange, I'm tring to figure out where in doPost to place the call to function which will process the new row. Both Browser.msgBox (commented in the above example) doesn't show any output neither the call to my function placed there. Any idea how to solve this problem?

2
What have you tried already? - Cooper
What makes you think that your function placed doesn't run. Show logs. - TheMaster
When I start the function from the menu in sheet it works (based on onEdit triger, copy the last row to another sheet, depending of contents of column C). The same function call placed in doPost just do nothing. - S. Hristov
I added this code: ... sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow]); distributeLast('C'); Logger.Log("distributeLast('C')"); return ContentService ... and here is the result after execution of html part (new row is added to the sheet, so doPost was executed): No logs found. Use Logger API to add logs to your project. - S. Hristov
@S.Hristov Can you update your code in the question to include the new code? - AMolina

2 Answers

1
votes

@St3ph Thank you for your help! The mistery is now revealed :-) In html part I have a menu to select the google table where the data is sending. All 3 tables in this menu are identicaly prepared and each table has its own unique https://script.google.com/macros/s/table__key/exec. For some reason doPost is executed from Table 2 wich chronologically was created a fiew days after Table 1 and a fiew days before Table 3. All "post processing" functions I've included in Table 1 (where is the relevant data for them) and they work fine as executed from menu in the table. I've totally commented the doPost of Table 1 and Table 3 and all 3 tables continue to receive data as previous. When publishing the all 3 apps (for my 3 tables) I've used my credentials and selected to be run as anonymous according to GitHub - jamiewilson/form-to-google-sheets. Apparently there are some duplications wich cause the strange behaviour.

0
votes

How is your web app published ? When you published your web app you can select to be run with your credential or by user credential Then select to be run anonymously or not.

Easy way for what you want to do is to run the app with your credential and by anonymous user. But If you do the fetch from a web page in JS it will not work. You will have to use JSONP to avoid 'cross-domain issues'.

Other solution is to use the Apps Script API from Javascript.

Send post data to a script published to run as you anonymously can only be done from a server side script. For JS it is 2 solutions above.