【问题标题】:Keep images when adding choices to Google Forms with Apps script使用 Apps 脚本向 Google 表单添加选项时保留图像
【发布时间】:2023-03-20 00:36:01
【问题描述】:

每次提交表单时,我都会使用谷歌脚本向表单中的复选框项目添加新选项。一些现有的复选框项附有用户在填写表单时看到的图像。

我的问题是每次我的脚本运行时,它都会从复选框项目中删除图像。有没有办法将图像附加到表单上,同时添加新的选择?

(部分)我的代码:

  // retrieve form-checkbox object
  var item = form.getItems(FormApp.ItemType.CHECKBOX)
    .filter(function(item){
      return item.getTitle() === 'Top 5 films';
    })[0].asCheckboxItem();
  
  //make 2 lists, 'choices' with all choices in the checkbox obj
  // 'existingChoicesTitles' with all the titles of the choices.
  var choices = item.getChoices();
  var existingChoicesTitles =  choices.map(function(value){
    return value.getValue();
  })

  //check if obj in list 'values' already exists in array 'choices, if not add to 
  //'choices'
  for (i = 0; i < values.length; i++) {
    if (existingChoicesTitles.includes(values[i]) == false){
      choices.push(item.createChoice(values[i]));
    }
  }
  //set 'choices' list as new list of choices
  item.setChoices(choices);

【问题讨论】:

    标签: google-apps-script google-forms


    【解决方案1】:

    很遗憾,目前没有办法做到这一点。

    有一个有效的功能请求,要求能够使用 Apps 脚本 FormApp 添加此类图像。

    Add Images to Form Items

    我建议您在问题上标记 ☆,让 Google 知道您想要此功能,并订阅更新。您可能还想在评论中添加您的经验和用例。


    在您的情况下,似乎要将项目添加到复选框列表中,Apps 脚本会重新生成所有复选框项目,并且由于 FormApp 不支持这些类型的图像,因此在重新生成它们时不包括它们.

    对于您的用例,除了简单地以这种方式使用图像(如果要使用 Apps 脚本进行修改)之外,似乎没有实际的解决方法。

    如果您愿意做一些额外的工作,您可能希望将表单实现为简单的web app。那么您将拥有几乎无限的灵活性和更多的功能。

    网络应用示例

    这是一个非常简单的 Web 应用示例,它显示了一个输入框和一个用于将信息发送到“后端”的按钮,在此示例中为 Code.gs

    Code.gs

    function doGet() {
      return HtmlService.createHtmlOutputFromFile("index")
    }
    
    function receiveInfo(formResponse) {
      Logger.log(formResponse)
    }
    

    index.html

    <!DOCTYPE html>
    <html>
      <body>
    
        <input type='text' id='input'></input>
        <button id=main>Send Info</button>
    
    <script>   
    function main(){
      const input = document.getElementById('input')
      const info = input.value
      google.script.run.receiveInfo(info)
    }
    
    const mainButton = document.getElementById("main")
    mainButton.addEventListener('click', () => main())
    </script>
    
      </body>
    </html>
    

    参考文献

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多