javascript 如何使用 Google Apps Script 在 Google 表单中获取 URL 参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21554204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you get URL parameters in a Google Form using Google Apps Script?
提问by Stan
So I've got a Google Form where I'd like to pass in a parameter from an email link like this:
所以我有一个 Google 表单,我想在其中传递来自电子邮件链接的参数,如下所示:
https://docs.google.com/URL/forms/d/LONGSTRING/viewform?id=12345
https://docs.google.com/URL/forms/d/LONGSTRING/viewform?id=12345
I'd like to be able to grab that id and pass it into the spreadsheet recording results. Now I've got writing to a spreadsheet down, but getting the id is problematic.
我希望能够获取该 ID 并将其传递到电子表格记录结果中。现在我已经写了一个电子表格,但是获取 id 是有问题的。
I've already tried:
我已经尝试过:
function doGet(e) {
var id = e.parameter.id;
Logger.log(id);
}
and
和
function doPost(e) {
var id = e.parameter.id;
Logger.log("do post " + id);
}
Both of which throw errors when I view the execution transcript, which I suspect is because they're designed to run within deployed webapps.
当我查看执行记录时,这两者都会抛出错误,我怀疑这是因为它们被设计为在部署的 web 应用程序中运行。
Additionally, I've tried using plain old javascript to do something like:
此外,我尝试使用普通的旧 javascript 来执行以下操作:
var formUrl = window.location.href;
var formId = formUrl.split('?id=')[1];
which also throws errors when you view the execution transcript .
当您查看执行成绩单时,这也会引发错误。
Any other ideas?? Maybe I can get the information in another way?
还有其他想法吗??也许我可以通过其他方式获取信息?
采纳答案by rpilkey
I think you can't use your own id, you have to use the id of the Google Form's "Item" object. You can figure out the id by viewing a prefilled form. In the Form edit screen, click "Responses", "Get Pre-filled URL". Fill in the field that you want to use and click Submit. On the new page, you are given a URL in "Share this link to pre-fill the responses" . Examine that url and you can see "get" arguments like ?entry.265525444=mytext . Replace mytext with whatever you want to be in the field.
我认为您不能使用自己的 id,您必须使用 Google Form 的“Item”对象的 id。您可以通过查看预填表格来找出 ID。在表单编辑屏幕中,单击“响应”、“获取预先填写的 URL”。填写您要使用的字段,然后单击提交。在新页面上,您将在“共享此链接以预先填写回复”中获得一个 URL。检查该 url,您可以看到“get”参数,如 ?entry.265525444=mytext 。将 mytext 替换为您想要在该字段中的任何内容。
This will display the field on your form, however. I myself was searching for how to make such a field readonly or invisible when I got here, but I hope this shows you a new direction to try.
但是,这将在您的表单上显示该字段。当我来到这里时,我自己正在寻找如何使这样的字段只读或不可见,但我希望这能向您展示一个新的尝试方向。
回答by RachelW
I am assuming you are trying to get the form id?
我假设您正在尝试获取表单 ID?
You will need to set up a trigger that will run on form submit.
您需要设置一个在表单提交时运行的触发器。
function onSubmit(){
var form = FormApp.getActiveForm();
var formID = form.getId();
}