动态添加选定的比例项目值 [英] Dynamically adding selected scale item values
问题描述
我正在创建一个表单,其中添加了几个 SCALE
类型的项目,我希望用户从1到3的比例中进行选择。
现在我想动态添加所有这些选定值,例如 sum = scaleA + ..scaleB + ..ScaleN
。
到目前为止,我已经设法获得所有 SCALE
项目,但是我无法获得 value
。
API 仅给出以下方法 getUpperBound()
和 getLowerBound()
其中我想获取用户当前选择的值。
有没有一种方法可以做到这一点?
注意:我想在填写表单时立即向用户显示选定值的总和。
我有这段代码e迄今为止写入
function myFunction(){
var form = FormApp.getActiveForm();
var items = form.getItems(FormApp.ItemType.SCALE); (item);
for(var i = 0; i< items.length; i ++)
Logger.log(items [i] .getId()+':Title'+ items [i] .getTitle +':ScaleItem'+ items [i] .asScaleItem());
将不得不使用谷歌应用程序脚本来创建自己的HTML服务。像这样: https://script.google.com/macros/s / AKfycby5b0Q2cuDSTl8VDIG60-9lnklAgeaZlXwmZq2slRG4h-fx6og / exec
来自Google的原生表格应用程序不能用于制作在表单生效时更新的动态表单。
Html文件:ScaleForm,代码来源: http://www.w3schools.com/tags/tag_output.asp
<!DOCTYPE HTML>
< html>
< head>
< base target =_ top>
< / head>
< body>
< div id =formDiv>
< form id =scaleFormoninput =x.value = parseInt(a.value)+ parseInt(b.value)>
0
< input type =rangeid =aname =avalue =0min =0max =3>
3< br>
+< br> 0
< input type =rangeid =bname =bvalue =0min =0max =3>
3< br>
=
< input type =buttonvalue =Submitonclick =formSubmit()>
< / form>
< / div>
< br>
值显示如下:
< div id =submittedValue>
< / div>
< script>
函数formSubmit(){
var formvalues = []
var d = new Date();
formvalues [0] = d.toString();
formvalues [1] = document.getElementById(a)。value
formvalues [2] = document.getElementById(b)。value
formvalues [3] = document.getElementById (c)。value
var sub = document.getElementById(submittedValue)
sub.innerHTML =Value 1:+ formvalues [1] +< br> Value 2: + formvalues [2] +< br>总计:+ formvalues [3]
google.script.run.recordValues(formvalues)
< /脚本>
< / body>
< / html>
Google脚本代码:
函数doGet(){
返回HtmlService.createTemplateFromFile('ScaleForm')
.evaluate()
.setTitle('Scale Form for stack overflow');
函数recordValues(formvalues){
var scale1 = formvalues [0]
var scale2 = formvalues [1]
var sum = formvalues [2]
//上述作业仅供参考,仅供参考不使用
var Ss = SpreadsheetApp.openById(在此处粘贴电子表格ID以记录值 )
var Sheet = Ss.getSheetByName(Responses)
Sheet.appendRow(formvalues)
如何执行此操作:
$ b
-
在scripts.google.com中创建一个脚本文件和一个名为ScaleForm的HTML文件。
-
将HTML代码,HTML文件和脚本复制到脚本文件。
-
使用publish菜单将它部署为一个web应用程序,您应该很好。
ol> In scripts.google.com create a script file and an HTML file named "ScaleForm".
Copy the HTML code the HTML file and script to the script file.
Use publish menu to deploy this as a web app and you should be good to go.
有关web应用程序的更多详细信息,请访问: https://developers.google.com/apps-script/guides/web
I am creating a form in which I have added few items of type SCALE
and I want the user to select from a scale of 1 to 3.
Now I want to dynamically add all these selected values such as the sum = selected value of scaleA + ..scaleB+ ..ScaleN
.
So far I have managed to get all the SCALE
items, but I am unable to get the value
that use has currently selected.
The API gives only following methods getUpperBound()
and getLowerBound()
where as I want to get the value the user has currently selected.
Is there a way I can achieve this?
Note: I want to display the user the sum of selected value right away as he is filling in the form.
The piece of code that I have written so far
function myFunction() {
var form = FormApp.getActiveForm();
var items = form.getItems(FormApp.ItemType.SCALE);
for (var i = 0; i < items.length; i++)
Logger.log(items[i].getId() + ':Title ' + items[i].getTitle()+ ':ScaleItem '+items[i].asScaleItem());
}
To make a dynamic form you will have to use google apps scripts to create your own from using HTML service. Like So: https://script.google.com/macros/s/AKfycby5b0Q2cuDSTl8VDIG60-9lnklAgeaZlXwmZq2slRG4h-fx6og/exec
Native form application from google cannot be used for making a dynamic form that updates while the form is live.
Html File: "ScaleForm", code source: http://www.w3schools.com/tags/tag_output.asp
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div id = "formDiv">
<form id = "scaleForm" oninput="x.value=parseInt(a.value)+parseInt(b.value)">
0
<input type="range" id="a" name="a" value="0" min ="0" max ="3">
3 <br>
+ <br> 0
<input type="range" id="b" name="b" value="0" min ="0" max ="3">
3 <br>
=
<output id = "c" name="x" for="a b"></output> <br>
<input type ="button" value ="Submit" onclick ="formSubmit()">
</form>
</div>
<br>
Values Submitted shown Below:
<div id="submittedValue">
</div>
<script>
function formSubmit(){
var formvalues = []
var d = new Date();
formvalues[0] = d.toString();
formvalues[1] = document.getElementById("a").value
formvalues[2] = document.getElementById("b").value
formvalues[3] = document.getElementById("c").value
var sub = document.getElementById("submittedValue")
sub.innerHTML = "Value 1: " +formvalues[1] +"<br>Value 2: "+formvalues[2] +"<br>Total: "+formvalues[3]
google.script.run.recordValues(formvalues)
}
</script>
</body>
</html>
Google Script code:
function doGet() {
return HtmlService.createTemplateFromFile('ScaleForm')
.evaluate()
.setTitle('Scale Form for stack overflow');
}
function recordValues(formvalues){
var scale1 =formvalues[0]
var scale2 = formvalues[1]
var sum = formvalues[2]
// The above assignments are for reference sake only they are not used
var Ss = SpreadsheetApp.openById("Paste your Spreadsheet ID here to record the values")
var Sheet = Ss.getSheetByName("Responses")
Sheet.appendRow(formvalues)
}
How to run this:
More details on web App can be found here: https://developers.google.com/apps-script/guides/web
这篇关于动态添加选定的比例项目值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!