如何使用 Google Apps Script 在 Google 表单中获取 URL 参数? [英] How do you get URL parameters in a Google Form using Google Apps Script?

查看:22
本文介绍了如何使用 Google Apps Script 在 Google 表单中获取 URL 参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 Google 表单,我想在其中传递来自电子邮件链接的参数,如下所示:

So I've got a Google Form where I'd like to pass in a parameter from an email link like this:

https://docs.google.com/URL/forms/d/LONGSTRING/viewform?id=12345

我希望能够获取该 ID 并将其传递到电子表格记录结果中.现在我已经写了一个电子表格,但获取 ID 是有问题的.

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.

我已经试过了:

function doGet(e) {
  var id = e.parameter.id;
  Logger.log(id);
}

function doPost(e) {
 var id = e.parameter.id;
 Logger.log("do post " + id);
}

当我查看执行记录时,这两个都会抛出错误,我怀疑这是因为它们被设计为在部署的 web 应用程序中运行.

Both of which throw errors when I view the execution transcript, which I suspect is because they're designed to run within deployed webapps.

此外,我尝试使用普通的旧 javascript 来执行以下操作:

Additionally, I've tried using plain old javascript to do something like:

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?

推荐答案

我觉得不能用自己的id,得用Google Form的Item"对象的id.您可以通过查看预填表格来找出 ID.在表单编辑屏幕中,单击响应"、获取预先填写的 URL".填写您要使用的字段,然后单击提交.在新页面上,您将在共享此链接以预先填写回复"中获得一个 URL.检查该 url,您可以看到get"参数,如 ?entry.265525444=mytext .将 mytext 替换为您想要在该字段中的任何内容.

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.

但是,这将在您的表单上显示该字段.当我来到这里时,我自己正在寻找如何使这样的字段只读或不可见,但我希望这为您提供了一个新的尝试方向.

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.

这篇关于如何使用 Google Apps Script 在 Google 表单中获取 URL 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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