如何使用Google Apps脚本动态更改Google表单上的现有文本项值 [英] How to dynamically change an existing text item value on Google Forms using Google Apps Script

查看:195
本文介绍了如何使用Google Apps脚本动态更改Google表单上的现有文本项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的Google表单,其中有一个TextItem,其标题为此位置在哪个位置?".

I have an existing Google Form in which there is a TextItem with a title "Which location was this performed at?".

无论何时加载(打开)表单,我都需要为该现有文本框设置位置值(loc)并将其显示给用户.

Whenever the form is loaded (opened), I need to set a location value (loc) to this existing textbox and show it to the user.

function populateMemberIds(loc){
    var form = FormApp.openById(formUrl);
    var questions = form.getItems();
    for (var i=0; i<questions.length; i++){
       if(questions[i].getTitle()=="Which location was this performed at?"){
       var textItem = questions[i].asTextItem();
       //I get stuck here
    }

}

我已经设置了openForm触发器,该触发器允许运行populateMemberIds函数以在每次加载表单时运行.同样,我需要的是使用位置值(loc)更改文本项的您的答案"部分的值.

I already setup the openForm trigger which allows to run the populateMemberIds function to be run on each form load. Again, what I need is to change the value of the "Your answer" section of the text item with the location value (loc).

我将不胜感激.

推荐答案

您无法修改用户填写的表单响应,可以通过编程方式创建表单响应,也可以在提交后编辑响应.当有人打开表单进行编辑而不是回答表单时,onOpen表单触发器就会运行[1]:

You can't modify a form response filled by a user, you can either create a form response programmatically or edit a response after being submitted. The onOpen form trigger runs when someone opens the form to edit it rather than answer it [1]:

当用户打开一个表单进行响应时,不会发生此事件,但是 而是当编辑者打开表单进行修改时.

This event does not occur when a user opens a form to respond, but rather when an editor opens the form to modify it.

此外,触发器函数附带一个已经定义的事件参数[1],因此您无法像使用loc参数那样设置自己的函数参数.

Moreover, triggers functions comes with an event parameter already defined [1] so you can't set your own function parameter(s) as you're doing with your loc parameter.

编辑

您可以以编程方式创建和提交表单响应[2],还可以从中获得带有预填表单的URL,以供用户完成[3].

You can programmatically create and submit a form response [2], from which you can also get a URL with a prefilled form for the user to finish [3].

function populateMemberIds(loc){
  var form = FormApp.openById("[FORM-ID]");
  var questions = form.getItems();
  var response = form.createResponse(); 

  for (var i=0; i<questions.length; i++){
    if(questions[i].getTitle()=="title form"){//Which location was this performed at?"){
      var textItem = questions[i].asTextItem();
      var itemResponse = textItem.createResponse(loc) ;
      response.withItemResponse(itemResponse);
    }
  }
  //Submit programmatically the form response
  response.submit();
  //URL with prefilled form response
  Logger.log(response.toPrefilledUrl()); 
}

function test () {
  populateMemberIds("US");
}

[1] https://developers.google.com/apps-script/guides/triggers/events#google_forms_events

[2] https://developers.google.com/apps-script/reference/forms/form-response

[3] https://developers.google. com/apps-script/reference/forms/form-response#toprefilledurl

这篇关于如何使用Google Apps脚本动态更改Google表单上的现有文本项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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