1

The goal is to send with the form data in Google Sheets to be recorded in the appropriate fields. I used this article, but my script gives an error:

{
"result": "error",
  "Error in: ": {
  "message": "Invalid argument: id",
  "name": "Exception",
  "fileName": "Code",
  "lineNumber": 20,
  ""smack": "\that Code:20 (handleResponse)\n\tat Code:2 (doGet)\n"
  }
}

Please help me to find out what error I made in Google Apps Script, or tell me how you can diagnose the error yourself.

Google Apps Script:

function doGet(e){
  return handleResponse(e);
}

//  Enter sheet name where data is to be written below
        var SHEET_NAME = "Table";

var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service

function handleResponse(e) {
  // shortly after my original solution Google announced the LockService[1]
  // this prevents concurrent access overwritting data
  // [1] http://googleappsdeveloper.blogspot.co.uk/2011/10/concurrency-and-google-apps-script.html
  // we want a public lock, one that locks for all invocations
  var lock = LockService.getPublicLock();
  lock.waitLock(30000);  // wait 30 seconds before conceding defeat.

  try {
    // next set where we write the data - you could write to multiple/alternate destinations
    var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
    var sheet = doc.getSheetByName(SHEET_NAME);

    // we'll assume header is in row 1 but you can override with header_row in GET/POST data
    var headRow = e.parameter.header_row || 1;
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow()+1; // get next row
    var row = []; 
    // loop through the header columns
    for (i in headers){
      if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
        row.push(new Date());
      } else { // else use header name to get data
        row.push(e.parameter[headers[i]]);
      }
    }
    // more efficient to set values as [][] array than individually
    sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
    // return json success results
    return ContentService
          .createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
          .setMimeType(ContentService.MimeType.JSON);
  } catch(e){
    // if error return this
    return ContentService
          .createTextOutput(JSON.stringify({"result":"error", "error": e}))
          .setMimeType(ContentService.MimeType.JSON);
  } finally { //release lock
    lock.releaseLock();
  }
}

function setup() {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    SCRIPT_PROP.setProperty("key", doc.getId());
}
3
  • I thought that your error message might be returned from Google Apps Script. If my understanding was correct, can you provide the information about "lineNumber": 20, of "fileName": "Code",? Although I couldn't understand about id, from "message": "Invalid argument: id",, how about confirming id again? If I misunderstood your situation, I apologize. Commented May 19, 2019 at 12:27
  • I do not understand where these values come from in Google Scripts, for the demonstration I removed almost all extraneous libraries and made the output of the data sent to the console on the client side, the data goes correctly, apparently the problem in processing data on the server side of Google Sheets. I also added the whole project to the public repository: github.com/Frost0x/Gatsby-MDBootstrap-FM.git Commented May 19, 2019 at 15:53
  • I apologize that my comment was not useful for your situation. Commented May 19, 2019 at 23:07

1 Answer 1

4

You are getting this error because you forgot to perform the addition of the Google Apps Script.

To Do this - Go to the “Run” menu and select “setup.”

Sign up to request clarification or add additional context in comments.

1 Comment

Run and Setup was the answer. This might also work for other google script error codes. I saved the code. I 'run' the code. I did not select Setup prior to Deploy. Selecting -- setup -- prior to Deploying was the key which allowed my form data to post to my Google sheet. Thank You. Thank you 🙏🏽 Thank you for this answer!!! My original question. How to fix {"result":"error","error":{"name":"Exception"}} in google apps scripts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.