Parameter in e.parameter is undefined

2020-05-07 18:33发布

I keep getting a message that the parameter is not defined. What am I doing wrong? I am new to Google Appscript and I am just trying to build a simple web application that stores First Name and Last Name to a spreadsheet.

Here is the link to the web-app: https://script.google.com/macros/s/AKfycbwwQts-izTP6MTWRM64948Gskf6MMktO07bTw_55qD9kZXOy17c/exec

function doGet(e){

    Logger.log(e);
    var FS = e.parameter.Fname;
    var LS = e.parameter.Lname;

    savedata(FS,LS);

}

function savedata(FS,LS) {
  var date = new date(); //gets new date
  var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1f2oIAuOt8PoAEtQW5zr6GJDwpPKl8doKhmECDEaHXl4/edit#gid=0'; 

  var worksheet = ss.getSheetByName("logs1"); 
  var cur_row = worksheet.getLastRow() + 1; 

  worksheet.getActiveRange("A" + cur_row).setValue(cur_row); 
  worksheet.getActiveRange("B" + cur_row).setValue(date);
  worksheet.getActiveRange("C" + cur_row).SetValue(FS); 
  worksheet.getActiveRange("D" + cur_row).SetValue(LS); 

}

1条回答
倾城 Initia
2楼-- · 2020-05-07 19:07

The event object's parameter property cannot be "undefined" on get request as event object always has the following structure (without any query parameters):

{
"parameter":{},
"contextPath":"",
"contentLength":-1,
"queryString":"",
"parameters":{}
}

Since you get the message that parameter is not defined, you most likely try to run doGet() from the editor - you should not do that as event object is constructed only if you access the url from the browser or send http request programmatically (in the last case not specifying query at all or mistyping a param won't result in this message as well).

Plus, getActiveRange() method does not accept any args, maybe you meant setActiveRange()? If so, you also need to wrap each Range reference in worksheet.getRange(yourRangeString) (and consider using cost-effective setValues(ArrayOfValues) method instead on range "A"+cur_row+":D"+cur_row).

Overall, your code should look like this (minus the test getSelf() function):

/**
 * Http GET request Trigger;
 * @param {Object} event object;
 * @returns {String} content to respond to getSelf();
 */
function doGet(e) {

  var f = e.parameter.Fname;
  var s = e.parameter.Lname;

  doSomething(f,s);

  //doGet() must return either HtmlOutput or TextOutput
  var content = ContentService.createTextOutput(JSON.stringify({Fname:f,Lname:s}));
  return content;
}

/**
 * Test function to programmatically invoke doGet();
 */
function getSelf() {
  //test query;
  var query = '?Fname=alice&Lname=blue';

  //getService().getUrl() returns script's public Url;
  var resp = UrlFetchApp.fetch(ScriptApp.getService().getUrl()+query);
  Logger.log(JSON.parse(resp));
}

/**
 * Saves values to spreadsheet;
 * @param {String} a first content to set;
 * @param {String} b second content to set;
 */
function doSomething(a,b) {

  var date = new Date();
  var ss = SpreadsheetApp.openByUrl( yourSpreadUrl ); 

  var ws = ss.getSheetByName( yourSheet );
  var cur_row = ws.getLastRow() + 1;
  var cur_rng = 'A'+cur_row+':D'+cur_row;

  ws.setActiveRange(ws.getRange(cur_rng)).setValues([[cur_row,date,a,b]]);
}

Btw, if you want others to be able to access your current deployment, be sure to change publishing setting from "only myself" to "anyone" or "anyone, even anonymous".

查看更多
登录 后发表回答