【问题标题】:set active sheet is not opening the sheet in UI设置活动工作表未在 UI 中打开工作表
【发布时间】:2021-04-07 14:01:30
【问题描述】:

我是整个编码方面的新手,所以我提前为任何菜鸟道歉。 我正在测试一个简单的 sn-p 代码,我想在其他一些函数中使用它,但我似乎无法让它按预期工作,我不确定我做错了什么。所以本质上我希望能够在 UI 中打开一个工作表选项卡,这样无论当前打开和激活哪个工作表,打开供用户使用的工作表都是已定义的工作表。

    function activateDashboard(){

WorkBook.App.setActiveSheet(RECIPEBOOK.Dashboard);
Logger.log(WorkBook.ActiveSheet.getSheetName())// returns the currently open sheet name
Logger.log(WorkBook.App.setActiveSheet(RECIPEBOOK.Dashboard).getSheetName())//returns dashboard which is the sheet is want to open
   };

我不确定setActiveSheet 是否会完成我想要的。也许这就是它不起作用的原因,或者是因为我做错了什么

【问题讨论】:

  • Here's the google apps script documentation 这是我们在 google-apps-script 标签上使用的代码。您似乎没有使用该代码。
  • Workbook.app 只是电子表格应用程序的一个变量。发帖之前忘记改回来了。我使用了变量,以及具有相同结果的电子表格应用程序。

标签: javascript google-apps-script google-sheets google-sheets-api


【解决方案1】:

您可以使用内置的SpreadsheetApp 来做到这一点。我对WorkBook.App 很陌生,但似乎他们与SpreadsheetApp 有类似的方法。

代码:

function activateDashboard() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  // returns the currently open sheet name - Should return "Sheet1"
  Logger.log(spreadsheet.getActiveSheet().getSheetName());
  // sets "Sheet2" as active
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet2'));
  // returns the currently open sheet name - Should return "Sheet2" after setActiveSheet
  Logger.log(spreadsheet.getActiveSheet().getSheetName());
}

样本数据:

日志:

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 2018-03-22
    • 2021-08-29
    • 2022-12-09
    相关资源
    最近更新 更多