google-apps-script - 从 Google 表格的下一个单元格打开 URL

标签 google-apps-script google-sheets

我正在使用谷歌表格。

我有 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/

相关文章:

xmlhttprequest - 谷歌表格 API 请求上的 CORS 授权

javascript - 如何在编辑表的单元格上方添加列

javascript - HtmlService 表不支持电子表格中的换行符和超链接

javascript - 谷歌脚本超时

javascript - 根据列值将一张 Google 工作表拆分为多张工作表 - 替换重复工作表

python - 如果我有一个except,我该如何重做?

google-sheets - 在 Google 表格中导入 HTML 时按特定字符串拆分列

google-apps-script - 有没有办法为 Google Drive 文件夹中的所有视频添加水印?

html - 将 HTML 内容渲染到 Google 电子表格

regex - 如何计算单元格是否在 Google 表格中的单词末尾包含 *