为什么autoResizeColumn()循环会冻结Google Apps脚本?
Why does an autoResizeColumn() loop seem to freeze a Google Apps Script?
我要做的就是调整电子表格中所有列的大小。这是一个独立的电子表格。我使用:
function format() {
var destination = SpreadsheetApp
.openByUrl("https://docs.google.com/spreadsheets/d/spreadsheetIdHere/edit#gid=0")
.getSheetByName("sheetName");
var columns = destination.getMaxColumns();
destination.setFrozenRows(1);
destination.setFrozenColumns(3);
for (var i = columns; i >= 1; i--) {
Logger.log(i)
destination.autoResizeColumn(i);
}
}
我已经尝试了for (var i = columns; i >= 1; i--)
和for (var i = 1; i <= columns; i++)
只是因为我不知道是什么引起的。我假设一个无限循环,但我完全删除了循环,它仍然运行了很长时间。
日志产生预期的结果,当我注释掉autoResize…大约一秒钟后。带回autoResize…脚本一直运行,甚至不改变任何列。
我试着拿出循环,把一个数字在autoResizeColumn(227)
和工作。把循环带回来,我被卡住了。
是autoResizeColums()只是太慢,还是我错过了什么?
第一部分
从问题:
从平():,脚本就一直运行,甚至不改变任何列。
电子表格操作有时捆绑在一起以提高性能,例如在多次调用Range.getValue()时。
如果您想在脚本结束之前看到脚本对电子表格所做的更改,您必须添加SpreadshsheetApp.flush()
。请记住,但是在循环中添加它在官方文档1中有一个不良实践的例子。
1:最佳实践
<标题> 第2部分从问题:
是autoResizeColums()只是太慢,还是我错过了什么?
总的来说,调用SpreadsheetApp和其他类似的服务是非常缓慢的。根据我自己的经验,我见过的最慢的调用是9秒,通过使用属性服务
标题>相关文章:
- Google Apps Script中的标准Javascript
- Google Apps 脚本为 getLastRow 抛出电子邮件失败通知,我做错了什么
- Google Apps 脚本在单元格中返回行值
- 在Google Apps上从JavaScript迁移到Python
- 在 Google Apps 脚本中,无法以编程方式创建触发器以从 onEdit 触发器发送电子邮件
- Google Apps 脚本中的格式化日期
- Google Apps 脚本:尝试根据单元格的背景颜色发送电子邮件
- Google Apps 脚本,用于根据一个单元格的值对另一个单元格进行条件颜色格式设置
- 如何转换SalesForce Api提供的UTC日期格式,并使用Google Apps脚本将其转换为本地日期和时间格式
- Google UiApp中的Google Apps脚本验证问题
- 添加“;密码”;键入Google Apps Script inputBox
- 找不到Google Apps脚本库嵌套函数
- HTTP POST 和 Google Apps Script 文件上传
- Google Apps Script 中基于 Twitter OAuth Pin 的授权“oob”
- Google-apps-script不会在onEdit函数中发送电子邮件
- 在 Google Apps Script Web App 中显示文件和文件夹
- 使用 Google Apps 脚本处理数组中输入元素中的多个文件
- 在 Google Apps 脚本中调用函数
- Google 电子表格:如何使用 Google Apps 脚本命名范围
- Google Apps 脚本:解析 XML 会导致错误:“无法在对象中找到函数 getChildren”