e.parameter中的参数未定义 [英] Parameter in e.parameter is undefined

查看:168
本文介绍了e.parameter中的参数未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到一条消息,提示未定义参数.我究竟做错了什么?我是Google Appscript的新手,我只是想构建一个简单的Web应用程序,将名字"和姓氏"存储到电子表格中.

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.

以下是网络应用程序的链接: https://script .google.com/macros/s/AKfycbwwQts-izTP6MTWRM64948Gskf6MMktO07bTw_55qD9kZXOy17c/exec

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); 

}

推荐答案

获取对象时,事件对象的parameter属性不能为"undefined",因为事件对象始终具有以下结构(没有任何查询参数):

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":{}
}

由于您收到未定义参数的消息,因此您很可能尝试从编辑器运行doGet()-仅当您从浏览器访问url或发送http请求时才构造事件对象,因此不应这样做以编程方式(在最后一种情况下完全不指定查询或错误地设置参数也不会导致此消息).

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).

另外,getActiveRange()方法不接受任何参数,也许您是说setActiveRange()?如果是这样,您还需要将每个Range引用都包装在worksheet.getRange(yourRangeString)中(并考虑在范围"A"+cur_row+":D"+cur_row上使用具有成本效益的setValues(ArrayOfValues)方法).

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).

总体而言,您的代码应如下所示(减去测试getSelf()函数):

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".

这篇关于e.parameter中的参数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆