使用Apps脚本向Google表单添加选项时保留图像 [英] Keep images when adding choices to Google Forms with Apps script

查看:0
本文介绍了使用Apps脚本向Google表单添加选项时保留图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的问题是,每次运行我的脚本时,它都会从复选框项中删除图像。是否有办法在向表单添加新选项的同时保留附加到表单的图像?

(部分)我的代码:

  // 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);

推荐答案

遗憾的是,目前无法做到这一点。

有一个活动的功能请求,要求能够使用应用程序脚本FormApp添加此类图像。

Add Images to Form Items

我建议你去标记这个问题上的☆,让谷歌知道你想要这个功能,并订阅更新。您可能还希望在评论中添加您的经验和用例。


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

对于您的用例,如果要使用Apps脚本修改图像,似乎没有实际的解决方法,只需以这种方式使用图像而不是

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

Web应用程序示例

这是一个非常简单的Web App示例,它显示了一个输入框和一个用于将信息发送到‘后端’的按钮,在本例中是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>

参考资料

这篇关于使用Apps脚本向Google表单添加选项时保留图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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