1

I am writing script to post data to spreadsheet but getting error, when testing the same on Postman:

CORS Error: The request has been blocked because of the CORS policy

Below is my script:

var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1UEzI8a8qVBqzVGuA4AZKs4eb4Y5bqOlIOJWwBDkEKb8/edit#gid=0");
var sheet = ss.getSheetByName('Items'); // be very careful ... it is the sheet name .. so it should match 

function doPost(e) {
    var action = e.parameter.action;

    if (action == 'addItem') {
        return addItem(e);
    }
}

function addItem(e) {
    var date = new Date();
    var id = "Item" + sheet.getLastRow(); // Item1
    var itemName = e.parameter.itemName;
    var brand = e.parameter.brand;

    sheet.appendRow([date, id, itemName, brand]);

    return ContentService.createTextOutput("Success").setMimeType(ContentService.MimeType.TEXT);
}
TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • 1
    the error is well self explained. `e` does not have a property `parameter`. What exactly do you want to get? – Marios Sep 30 '20 at 08:28
  • How are you calling your `doPost(e)` function, and from where? – Rafa Guillermo Sep 30 '20 at 08:45
  • Removed first error, as it is irrelevant. See my and we answer [here](https://stackoverflow.com/questions/16089041/how-can-i-test-a-trigger-function-in-gas) – TheMaster Sep 30 '20 at 11:30

1 Answers1

0

You need to return a value from doPost rather than addItem.

This always trips me up, too. If there is no return value, you get a CORS warning and the POST fails.

For this reason, it can be difficult to tell if the script has failed or if there's a problem with the post request. Try wrapping everything in try, catch, finally to ensure you always return a value.

Try this. It's a bit verbose but it should ensure that you definitely get a response to your POST request and that response contains all the information you need to see what's going wrong.

function doPost(e) {
  var returnJson = {
    success: false,
    errors: [],
    returnValue: null,
    debug: [],
  };

  try {
    if (e.parameter && e.parameter.action) {
      var action = e.parameter.action;
      if (action == "addItem") {
        var itemAdded = addItem(e);
        returnJson.debug.push(itemAdded);
      }
      returnJson.success = true;
    } else {
      returnJson.debug.push("No value 'action' in e.parameter");
    }
  } catch (err) {
    returnJson.errors.push(err);
  } finally {
    return ContentService.createTextOutput(
      JSON.stringify(returnJson)
    ).setMimeType(ContentService.MimeType.JSON);
  }
}

function addItem(e) {
  var returnJson = {
    success: false,
    errors: [],
    returnValue: null,
    debug: [],
  };
  try {
    var date = new Date();
    var id = "Item" + sheet.getLastRow(); // Item1
    var itemName = e.parameter.itemName;
    var brand = e.parameter.brand;

    var ss = SpreadsheetApp.openByUrl(
      "https://docs.google.com/spreadsheets/d/1UEzI8a8qVBqzVGuA4AZKs4eb4Y5bqOlIOJWwBDkEKb8/edit#gid=0"
    );
    var sheet = ss.getSheetByName("Items"); // be very careful ... it is the sheet name .. so it should match

    sheet.appendRow([date, id, itemName, brand]);
    returnJson.success = true;
  } catch (err) {
    returnJson.errors.push(err);
  } finally {
    return returnJson;
  }
}




Dave Cook
  • 617
  • 1
  • 5
  • 17