我正在使用谷歌表格。
我有 2 个按钮:按钮开始 (单击时会打开 B2 单元格中的第一个 URL)和 按钮下一步 (单击时应打开 B3 URL,然后是 B4,然后是 B5,依此类推)。问题出在 Next 函数上。
有人可以帮我解决这个问题吗?
function start(){
var i=2;
open(i);
}
function open(i){
var selection = SpreadsheetApp.getActiveSheet().getRange('B'+ i).getValue();
var html = "<script>window.open('" + selection + "');google.script.host.close();</script>";
var userInterface = HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModalDialog(userInterface, 'Opening page...');
}
function next(i){
i++;
open(i);
}
最佳答案
当您调用 function next(i)
, i
没有定义。
如果要存储i
的值之间的两个脚本调用,你需要使用 PropertiesService .特此,ScriptProperties允许您在两个脚本调用之间保存变量的值。
sample :
function start(){
var i=2;
PropertiesService.getScriptProperties().setProperty("i", i)
open(i);
}
function open(i){
var selection = SpreadsheetApp.getActiveSheet().getRange('B'+ i).getValue();
var html = "<script>window.open('" + selection + "');google.script.host.close();</script>";
var userInterface = HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModalDialog(userInterface, 'Opening page...');
}
function next(){
var i=PropertiesService.getScriptProperties().getProperty("i")
i++;
PropertiesService.getScriptProperties().setProperty("i", i)
open(i);
}
关于google-apps-script - 从 Google 表格的下一个单元格打开 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58261711/