相当于 Google Apps 脚本中的“ScreenUpdating”(相当于 VBA-GAS)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17749869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent of “ScreenUpdating” in Google Apps Script (equivalent VBA-GAS )
提问by Frank Montemorano
I'm looking for the equivalent VBA-GAS of:
我正在寻找等效的 VBA-GAS:
Application.ScreenUpdating = False
I'm running a very long macro in one of my Google Spreadsheets and it takes at least 30 seconds to finish it every time. It would be helpful if the macro didn't refresh the screen after every line of code.
我在我的一个 Google 电子表格中运行了一个很长的宏,每次至少需要 30 秒才能完成。如果宏在每行代码之后都没有刷新屏幕,那将会很有帮助。
采纳答案by Zig Mandel
There isnt. However you should batch all setValues into range writes (ideally a single range write) which will help here.
Only call SpreadsheetApp.flush() at the very end.
没有。但是,您应该将所有 setValues 批量写入范围写入(理想情况下是单个范围写入),这将在这里有所帮助。
只在最后调用 SpreadsheetApp.flush()。
回答by FrinkTheBrave
I ended up creating a second sheet called 'Start' which just had A1='Please wait...'. The sheet I display all my data on is called 'Report'
我最终创建了第二张名为“开始”的工作表,其中只有 A1='请稍候...'。我在其上显示所有数据的工作表称为“报告”
Then used:
然后使用:
//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();
回答by tristeguero
The way I got around this is by bypassing every instance of "setActiveSheet" or "activate."
我解决这个问题的方法是绕过“setActiveSheet”或“activate”的每个实例。
For example:
例如:
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);
can be shortened to:
可以缩短为:
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('Sheet1!A2').copyTo(spreadsheet.getRange('Sheet2!B4'), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
and can be run from any sheet on the document without changing the selected sheet or cell.
并且可以从文档上的任何工作表运行,而无需更改所选工作表或单元格。
There may be a reason for Google to automatically add this redundancy, but this does appear to work in every case I've used it in.
Google 可能有理由自动添加此冗余,但这似乎在我使用过的每种情况下都有效。
回答by Warren Schmidenberg
While I understand that the below does not necessarily speed up the macro, it makes for a better UX and is important to note. For many function calls and updates, VBA first requires that we "unhide" the respective sheet:
虽然我知道下面的内容不一定会加速宏,但它可以提供更好的用户体验,并且需要注意。对于许多函数调用和更新,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
The same is not required for many of the Google Apps Script Classes:
许多 Google Apps 脚本类不需要相同的内容:
// Hide the sheet, write to it, show it
var sheet = SpreadsheetApp.getActive().getSheetByName('Report').hideSheet();
sheet.appendRow(['Blah']);
sheet.showSheet();