将URL参数传递到Web App脚本中 [英] Pass URL parameter into Web App script

查看:94
本文介绍了将URL参数传递到Web App脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题让我想拔掉头发.我正在尝试将URL参数传递给Google Apps脚本,其中包含要显示的数据的行ID(来自电子表格).我的参数是story.但是,无论尝试什么,我都会遇到各种各样的错误.最新的是:

This problem has me wanting to pull my hair out. I am trying to pass a URL parameter to Google Apps Script containing the row ID (from a spreadsheet) for the data I'm wanting to present. My parameter is story. However, I'm getting all sorts of errors no matter what I try. The latest being:

TypeError:无法从未定义中读取属性"parameter". (第2行,文件代码",项目"singleStory")

TypeError: Cannot read property "parameter" from undefined. (line 2, file "Code", project "singleStory")

下面是我的Code.gs和Index.html文件(该项目尚未完成.这就是我目前所处的位置.)

Below are my Code.gs and Index.html files (The project is not complete. This is just where I'm at so far.)

Code.gs

function doGet(e) {
    var i = e.parameter.story;
    return HtmlService
      .createTemplateFromFile('Index')
      .evaluate()
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);

 function getData() {
 return SpreadsheetApp
      .openById("1Z582cnr03fkLC7xCca4pcj7QwDtSCS4KGneyFKMgdyo")
      .getSheetByName("StoryTopics")
      .getDataRange()
      .getValues();
 }
}

Index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
  <? var data = doGet(); ?>

<div id="container">
<div id="header">
<div id="topicname">
    <?= data[i][0] ?>
</div>
<div id="todaysdate">
<p>As of: <?= new Date() ?></p>
</div>
<div id="topictype">
<?= data[i][1] ?>
</div>
<div id="locale">
<?= data[i][2] ?>
</div>
</div>
<div id="contact_container">
<div id="subheader">
</div>
<div id="contact_name">
<?= data[i][3] ?>
</div>
<div id="contact_title">
<?= data[i][4] ?>, <?= data[i][5] ?>
</div>
<div id="contact_email">
<?= data[i][6] ?>
</div>
<div id="contact_phone">
<?= data[i][7] ?>
</div>
</div>
<div id="action_container">
<div id="subheader">
</div>
<table id="action_table">
<tr>
<td>
Date:
</td>
<td>
Type:
</td>
<td>
Description:
</td>
</tr>
</table>
</div>
<div id="story_container">
<div id="story_title">
</div>
<div id="story_content">
</div>
</div>
</div>
</body>
</html>

我要测试的URL +参数是:

The URL + parameter I'm using to test is:

https://script.google.com/a/macros/--DOMAIN--/s/--SCRIPTID--/exec?story=3

有人能给我一些为什么它不起作用的想法吗?

Can anyone give me some ideas on why it's not working?

推荐答案

TypeError:无法从未定义中读取属性"parameter". (第2行,文件代码",项目"singleStory")

TypeError: Cannot read property "parameter" from undefined. (line 2, file "Code", project "singleStory")

这告诉您希望包含命名属性"parameter"的对象是未定义的.

This tells you that the object you expect to contain the named property "parameter" is undefined.

查看您的代码(e.parameter.story),这是指事件参数e.对于不熟悉此功能的用户,在已部署的Web应用程序中调用它时,专门命名的doGet()函数将收到事件参数,在此示例中为e.)

Looking at your code (e.parameter.story), that's referring to the event parameter e. For those unfamiliar with this, when it's invoked in a deployed Web App, the specially-named doGet() function receives an event parameter, which is named e in this example.)

您可能会看到的一个原因是,如果您正在调试器中运行该功能,而不是将其作为Web应用程序进行测试.凯文(Kevin),情况并非如此,因为您是在浏览器中输入URL来尝试启动Web应用程序.

One reason that you might see this is if you're running the function from the debugger, not testing it as a web app. That's not your case, Kevin, since you are entering a URL into the browser to try to launch the web app.

问题出在模板HTML 中. HTML中的第一个语句是:

The problem turns out to be in the Templated HTML. The very first statement in your HTML is:

<? var data = doGet(); ?>

这会在我们尝试.evaluate()模板时引起麻烦,因为它重新调用了doGet()函数,这次没有任何事件对象.并且是 时间发出错误消息.

This causes trouble as soon as we attempt to .evaluate() the template, as it re-invokes the doGet() function, this time without any event object. And it's that time around that spits out the error message.

相反,您可能打算这样做是为了从电子表格中获取数据:

You likely meant to do this, instead, to get the data from your spreadsheet:

<? var data = getData(); ?>

进行适当的更改后,您现在会收到事件参数e(您一直在执行...),代码继续进行.直到下一个错误:

With that change in place, you now receive the event parameter e (which you always did...) and the code carries on. Until the next bug:

ReferenceError:未定义"i". (第3行,文件代码")

ReferenceError: "i" is not defined. (line 3, file "Code")

现在事情变得棘手,因为在Code.gs中针对此语句报告了错误:

Now things get tricky, because the error is being reported against this statement in Code.gs:

return HtmlService
    .createTemplateFromFile('Index')
    .evaluate()
    .setSandboxMode(HtmlService.SandboxMode.IFRAME);

但是等等! i不是仅在第2行中定义的吗?是的,它是...但是,此错误与evaluate()试图填写模板有关,并且您的代码尚未为此设置i.您将需要分手此语句以首先获取模板,然后为其设置参数,最后对其进行评估.像这样:

But wait! Wasn't i just defined in line 2? Yes, it was... However this error is about evaluate() trying to fill out the template, and your code doesn't yet set i for that. You'll need to break up this statement to first get the template, then set parameters for it, and finally evaluate it. Like this:

function doGet(e) {
  var i = e.parameter.story;

  // First, get the template
  var template = HtmlService.createTemplateFromFile('Index');

  // Second, set template parameters
  template.i = i;

  // Finally, evaluate() the template
  return template.evaluate()
                 .setSandboxMode(HtmlService.SandboxMode.IFRAME);

}  // <-- You had this on the other side of 'getData()'

那应该让你滚动.您还有其他错误:

That should get you rolling. You've got other errors:

  • 阻止功能是错误的,您希望getData()在全局范围内,而不是在doGet()范围内.
  • 您可能必须调试模板,然后按照中的指导进行操作调试模板.
  • 模板代码包含在<? thusly ?>中,而不包含在<?= like this ?>中.
  • The blocking of your functions is wrong, you want getData() at global scope, not within doGet().
  • You'll probably have to debug your template, follow the guidance in Debugging templates.
  • Templated code is enclosed <? thusly ?>, not <?= like this ?>.

这篇关于将URL参数传递到Web App脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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