[英]How to call Google Script function inside the Javascript For loop
我有一个Google Script函数,该函数根据Javascript中的值填充单元格。GS函数在Javascript“ For”循环内调用。 当我运行代码时,直到“ For”循环的所有增量完成运行之前,才填充单元格。
神奇的是,在For循环完成之后,GS函数开始填充相关单元格(以某种方式记住所有动态值)。 但是,并非所有预期的单元都被填充,而且顺序也不正确。
尝试使用.flush()-没有帮助
GS功能:
function rolesInputer(role, i){
var rolesInputer = role;
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var column = 3+i;
var cell = sheet.getRange(17,column);
cell.setValue(role);
}
JS功能:
function saveInput() {
var i;
for (i = 1; i <= dataEncoded; i++) {
sleep(1000);
var temprole = "role" + (i);
var roleSelect = document.getElementById(temprole);
var roleSelected = roleSelect.options[roleSelect.selectedIndex].value;
google.script.run.withSuccessHandler(roleSelected, i).rolesInputer(roleSelected, i);
alert("executed");
}
google.script.host.close();
}
google.script.run()
在HTML端将值发送到Google Apps脚本端。 如果我的理解是正确的,那么该修改如何? 请认为这只是几个答案之一。
首先,关于您接下来的问题,我认为您问题的原因是google.script.run()
通过异步处理工作。
神奇的是,在For循环完成之后,GS函数开始填充相关单元格(以某种方式记住所有动态值)。 但是,并非所有预期的单元都被填充,而且顺序也不正确。
为了避免这种情况,我认为您的情况有两种模式。
在这种模式下,检索所有值后,这些值将发送到Google Apps脚本。 测试此模式时,请进行以下修改。
function rolesInputer(values){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange(17, 3, 1, values.length).setValues([values]);
}
我使用<select>...</select>
作为样本值。 在此示例中,当打开HTML时,将运行脚本。
<select id="role1"><option value="role1" selected>role1</option></select>
<select id="role2"><option value="role2" selected>role2</option></select>
<select id="role3"><option value="role3" selected>role3</option></select>
<select id="role4"><option value="role4" selected>role4</option></select>
<select id="role5"><option value="role5" selected>role5</option></select>
<script>
function saveInput() {
var dataEncoded = 5;
var values = [];
for (var i = 1; i <= dataEncoded; i++) {
var temprole = "role" + (i);
var roleSelect = document.getElementById(temprole);
var roleSelected = roleSelect.options[roleSelect.selectedIndex].value;
values.push(roleSelected);
}
google.script.run.withSuccessHandler(() => {google.script.host.close()}).rolesInputer(values);
}
saveInput();
</script>
在这种模式下,该值会使用for循环将每个值发送到Google Apps脚本。 测试此模式时,请进行以下修改。
在这种模式下,不会修改Google Apps脚本。
在此示例中,当打开HTML时,将运行脚本。
<select id="role1"><option value="role1" selected>role1</option></select>
<select id="role2"><option value="role2" selected>role2</option></select>
<select id="role3"><option value="role3" selected>role3</option></select>
<select id="role4"><option value="role4" selected>role4</option></select>
<select id="role5"><option value="role5" selected>role5</option></select>
<script>
function work(roleSelected, i) {
return new Promise((resolve, reject) => {
google.script.run.withSuccessHandler(() => resolve()).rolesInputer(roleSelected, i);
});
}
async function saveInput() {
var dataEncoded = 5;
for (var i = 1; i <= dataEncoded; i++) {
var temprole = "role" + (i);
var roleSelect = document.getElementById(temprole);
var roleSelected = roleSelect.options[roleSelect.selectedIndex].value;
await work(roleSelected, i);
}
}
saveInput().then(() => google.script.host.close());
</script>
google.script.run是HTML服务页面中可用的异步客户端JavaScript API,可以调用服务器端Apps脚本函数。
如果这对您的情况没有帮助,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.