相当于“;屏幕更新”;在Google应用程序脚本中(相当于VBA-GAS)

相当于“;屏幕更新”;在Google应用程序脚本中(相当于VBA-GAS),vba,google-apps-script,refresh,spreadsheet,updating,Vba,Google Apps Script,Refresh,Spreadsheet,Updating,我正在寻找等效的VBA-GAS: Application.ScreenUpdating = False 我在我的谷歌电子表格中运行了一个很长的宏,每次至少需要30秒才能完成。如果宏在每行代码之后都不刷新屏幕,这将非常有用 没有。但是,您应该将所有设置值批处理到范围写入中(理想情况下是单个范围写入),这在这里会有所帮助。只在最后调用SpreadsheetApp.flush()。我创建了第二个名为“开始”的工作表,其中只有A1='请稍候…'。显示所有数据的工作表称为“报告” 然后使用: //Hid

我正在寻找等效的VBA-GAS:

Application.ScreenUpdating = False

我在我的谷歌电子表格中运行了一个很长的宏,每次至少需要30秒才能完成。如果宏在每行代码之后都不刷新屏幕,这将非常有用

没有。但是,您应该将所有设置值批处理到范围写入中(理想情况下是单个范围写入),这在这里会有所帮助。
只在最后调用SpreadsheetApp.flush()。

我创建了第二个名为“开始”的工作表,其中只有A1='请稍候…'。显示所有数据的工作表称为“报告”

然后使用:

//Hide Report sheet
SpreadsheetApp.getActive().getSheetByName("Start").showSheet();
var sheet = SpreadsheetApp.getActive().getSheetByName("Report");
sheet.hideSheet();

//Code to update sheet here
sheet.appendRow('Blah','Blah','Blah');

//Show Report sheet
sheet.showSheet();
SpreadsheetApp.getActive().setActiveSheet(sheet);
SpreadsheetApp.getActive().getSheetByName("Start").hideSheet();

我绕过这个问题的方法是绕过“setActiveSheet”或“activate”的每个实例

例如:

var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A2').activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet2'), true);
spreadsheet.getRange('B4').activate();
spreadsheet.getRange('Sheet1!A2').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
可缩短为:

var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('Sheet1!A2').copyTo(spreadsheet.getRange('Sheet2!B4'), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
可以从文档上的任何工作表运行,而无需更改选定的工作表或单元格


谷歌自动添加这种冗余可能是有原因的,但这似乎在我使用过的每种情况下都有效。

虽然我理解下面的内容不一定会加快宏的速度,但它有助于实现更好的用户体验,值得注意。 对于许多函数调用和更新,VBA首先要求“取消隐藏”相应的工作表:

' Unhide the sheet, write to it, hide it '
Dim lastrow As Long

Sheets("Report").Visible = True
Sheets("Report").Select

' Append a Row '
lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
ActiveSheet.Cells(lastrow, "A").Value = "Blah"

Sheets("Report").Select
ActiveWindow.SelectedSheets.Visible = False
许多Google应用程序脚本类都不需要这样做:

// Hide the sheet, write to it, show it
var sheet = SpreadsheetApp.getActive().getSheetByName('Report').hideSheet();
sheet.appendRow(['Blah']);
sheet.showSheet();

你能再解释一下吗?如果我正在读取和更新单个列,我是否应该将该列作为范围并读取更新范围?我是否使用myrange.getRange(1,2).setValue('Title');而不是sheet.getRange(1,2).setValue('Title');还是说我太笨了?这有点疏忽了谷歌的职责。批量写入单元格适用于单个工作表,但不适用于在电子表格中添加、删除或重新排序工作表。当动画试图跟上时所产生的混乱是不好看的。不幸的是,这种方法不能提高整体宏速度=(