我使用具有容量的选项的Google Apps脚本创建了一个Web表单。
在下面的示例中,有一个问题要求游客选择芝士蛋糕或巧克力蛋糕。假设我只有两块芝士蛋糕和三块巧克力蛋糕,如果两个访客已经选择了芝士蛋糕,我想从表格中删除芝士蛋糕的选项,并使该选项不可见且不可选拔仅巧克力蛋糕。
然后,我应该如何实现此类选择
使用Google Apps脚本具有容量的选项?
请注意,但我想创建一个自定义的Web表单,这次我不为此使用Google表单。
编辑
以下链接将显示该程序如何在电子表格上保存数据:
index.htex.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form class="" action="<?!= getScriptUrl(); ?>" method="post">
<div>
<h1 id="Question">
Choose either cheesecake or chocolate cake.
</h1>
<select id="" name="cake" class="form-control">
<option value="cheesecake">cheesecake</option>
<option value="chocolate_cake">chocolate_cake</option>
</select>
</div>
<div class="form-submit">
<input type="submit" name="" value="Submit">
</div>
</form>
</body>
</html>
code.gs
function doGet(){
return HtmlService.createTemplateFromFile("index").evaluate();
}
function getScriptUrl() {
var url = ScriptApp.getService().getUrl();
Logger.log(url);
return url;
}
function doPost(e){
var sh = SpreadsheetApp.openById("11nE1yL24HamfbAbeRjoQV6SE0ecq6rCx1WlbQZ8N8R0").getSheets()[0];
sh.appendRow([String(e.parameters.cake)]);
return HtmlService.createHtmlOutput('<b>Thank you!</b>');
}
I created a web form using Google Apps Script that has options with capacity.
In the following example, there is a question to ask visitors to choose cheesecake or chocolate cake. Suppose that I have only two pieces of cheesecake and three pieces of chocolate cake, and if two visitors has already choose the cheesecake, I want to remove the option of cheesecake from the form and make that option invisible and thus unselectable, showing the option of chocolate cake only.
Then, how should I implement such a select
question whose options have capacity using Google Apps Script?
Note but I want to create a custom web form, and that this time I do NOT use Google Forms for that purpose.
EDIT
The following link will show how this programme saves data on a spreadsheet: https://docs.google.com/spreadsheets/d/11nE1yL24HamfbAbeRjoQV6SE0ecq6rCx1WlbQZ8N8R0/edit?usp=sharing
index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form class="" action="<?!= getScriptUrl(); ?>" method="post">
<div>
<h1 id="Question">
Choose either cheesecake or chocolate cake.
</h1>
<select id="" name="cake" class="form-control">
<option value="cheesecake">cheesecake</option>
<option value="chocolate_cake">chocolate_cake</option>
</select>
</div>
<div class="form-submit">
<input type="submit" name="" value="Submit">
</div>
</form>
</body>
</html>
code.gs
function doGet(){
return HtmlService.createTemplateFromFile("index").evaluate();
}
function getScriptUrl() {
var url = ScriptApp.getService().getUrl();
Logger.log(url);
return url;
}
function doPost(e){
var sh = SpreadsheetApp.openById("11nE1yL24HamfbAbeRjoQV6SE0ecq6rCx1WlbQZ8N8R0").getSheets()[0];
sh.appendRow([String(e.parameters.cake)]);
return HtmlService.createHtmlOutput('<b>Thank you!</b>');
}
发布评论
评论(2)
如果要根据数量删除选项,则应将每个蛋糕的数量保存到纸上。
在这里,我修改了您的代码并添加了一些功能:
code.gs:
index.html:
在这里,我创建了一个名为“ 库存”的表格:
< a href =“ https://i.sstatic.net/ra7y1.png” rel =“ nofollow noreferrer”>
每个人每次订购蛋糕时,数量会减少1。
< img src =“ https://i.sstatic.net/6t4fw.gif” alt =“在此处输入图像说明”>
当蛋糕数量到达0时,脚本将从下拉列表中删除芝士蛋糕:
注意:您可以复制并粘贴上面的代码,但您需要一个名为库存的表格,其中包括上面的示例中的内容和格式相同。
参考:
If you want to remove an option based on quantity, then you should save the quantity of each cake to a Sheet.
Here I modified your code and added some features:
Code.gs:
index.html:
Here I created a sheet named "Inventory":
and each time a person orders a cake, the quantity is reduced by 1.
When the quantity of cake reaches 0, the script will remove the cheesecake from the drop down list:
Note: You can copy and paste the code above but you need a Sheet named Inventory with the same content and format from my example above.
References:
我相信您当前的情况和目标如下。
1
。0
时,您不想选择它。在这种情况下,以下示例脚本怎么样?
样本电子表格
为了测试,使用此脚本中的 ,使用以下电子表格。该表是电子表格的第一张选项卡。
示例脚本,
请复制并将以下脚本粘贴到脚本编辑器。而且,请反映Web应用程序的最新脚本。
Google Apps脚本方面:
code.gs
html&amp; JavaScript方面:
index.html
测试
请访问在修改后的脚本中反映的已部署的Web应用程序。这样,您可以看到下拉列表。
在此样品中,芝士蛋糕有2个。选择芝士蛋糕并单击“提交”按钮时,电子表格上的芝士蛋糕数量从2到1。选择芝士蛋糕并再次单击按钮时,您可以确认该选项。不能选择芝士蛋糕。
我认为这可能是您的预期结果。
注意
修改Google Apps脚本时,请将部署修改为新版本。这样,修改后的脚本反映在Web应用程序中。请小心。
此示例脚本是一个简单的脚本。因此,请为您的实际情况进行修改。
参考
I believe your current situation and goal are as follows.
1
from cheesecake in the Spreadsheet.0
, you don't want to select it.In this case, how about the following sample script?
Sample spreadsheet
In this script, in order to test, the following Spreadsheet is used. This sheet is the 1st tab of the Spreadsheet.
Sample script
Please copy and paste the following scripts to the script editor. And, please reflect the latest script to the Web Apps.
Google Apps Script side:
Code.gs
HTML & Javascript side:
index.html
Testing
Please access the deployed Web Apps reflected in the modified script. By this, you can see the dropdown list.
In this sample, cheesecake has 2. When you select a cheesecake and click the "Submit" button, the number of cheesecakes on the Spreadsheet becomes from 2 to 1. When you select cheesecake and click the button again, you can confirm that the option of cheesecake cannot be selected.
I thought that this might be your expected result.
Note
When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
This sample script is a simple script. So, please modify this for your actual situation.
Reference