Google App脚本getActiveSheet返回最左边的工作表,而不是活动工作表 [英] Google App Script getActiveSheet returns leftmost sheet, not active sheet

查看:58
本文介绍了Google App脚本getActiveSheet返回最左边的工作表,而不是活动工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我猜这个问题是最类似于我当前的问题.但基本上,我有一个Google表格文件,其底部具有多个表格.我希望用户可以在他们当时打开的任何工作表上执行此脚本.

I guess this question is the most similar to my current question. But basically, I have a google sheets file that has several sheets along the bottom. I would like for the user to execute this script on whatever sheet they have open at the time.

就像之前的问题一样,根据此内容说文档,活动工作表是在电子表格UI中显示的工作表.我认为这意味着,如果我在另一个标签中打开了Google工作表文件,则当前可供我选择和查看的工作表将是活动工作表.

And like the previous question said according to this documentation, the active sheet is the one that is being displayed in the spreadsheet UI. I assume this means if I have the google sheets file open in another tab, that the sheet currently selected and viewable by me is intended to be the active sheet.

但是,返回的实际活动工作表始终是底部选项卡上最左侧的工作表.我已经通过切换工作表的顺序来确认.因此,无论我实际上打开了哪些工作表并在用户界面中看到它,它总是会得到最左侧的工作表.这是得到它的线.

However, the actual active sheet returned is always the leftmost sheet along the bottom tabs. I've confirmed it by switching the order of the sheets. So no matter what sheet I actually have open and visible in the UI, it always gets the leftmost sheet. Here is the line that gets it.

var Sheet = SpreadsheetApp.openById("redactedid").getActiveSheet()

这是App Script的已知错误吗?还是文档错误?如果是的话,是否有解决方法来获取UI中当前打开的工作表?我不希望任何人都必须在代码中对工作表名称进行硬编码,因为文件不断变化并且正在添加工作表.

Is this a known error with App Script? Or is the documentation wrong? If it is, is there a workaround for getting the currently open sheet in the UI? I don't want anyone to have to hardcode a sheetname in the code as the file is constantly changing and sheets are being added.

推荐答案

注意:

要使用 getActiveSheet()或任何活动对象,请调用

Notes:

To use getActiveSheet() or any active object, the calling

  • 脚本必须绑定到电子表格
  • 应该从菜单,按钮或宏键盘快捷键而不是从webapp或API调用脚本功能(脚本编辑器也可以,但不是首选).
  • 电子表格必须在用户界面/浏览器中(最好是在活动/当前选定的窗口中)打开/可见.
  • 以前所有获取对象的调用都必须使用 active .例如,要获得有效范围,您应该首先具有有效的电子表格=>活动表=>活动范围(活动对象链).如果任何调用都不是对活动对象的调用(例如,如果您在中间步骤中使用了 getSheetByName()而不是 getActiveSheet()),则最终调用很可能会默认为默认对象(第一张纸A1).
  • Script must be bound to the Spreadsheet
  • Script function should be invoked from menu or button or macro keyboard shortcuts and NOT from webapp or API( Script editor also works, but not preferred).
  • Spreadsheet must be open/visible in the user interface/browser preferably in the active/currently selected window.
  • All previous calls to get objects must have used active. For eg, To get active range, You should have first got active spreadsheet => active sheet => active range(the chain of active objects). If any of the calls is not to the active object(say if you used getSheetByName() instead of getActiveSheet() in the middle step), the final call is likely to default to the default object(First sheet A1).
  • 在这里,要获取 Spreadsheet ,请使用 SpreadsheetApp.getActive()而不是 SpreadsheetApp.openById(),以便使 getActiveSheet()将位于活动对象的链下.
  • Here, To get Spreadsheet, use SpreadsheetApp.getActive() instead of SpreadsheetApp.openById(), So that getActiveSheet() will be under the chain of active objects.
const sheet = SpreadsheetApp.getActive().getActiveSheet();

这篇关于Google App脚本getActiveSheet返回最左边的工作表,而不是活动工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆