google-apps-script - try/catch 不适用于 google 脚本单元格验证错误
问题描述
我在具有验证的单元格中的脚本中有一个 setValue() 操作,如果我使用不在列表中的值,则会引发错误。没关系。但是我的问题是,当我尝试/捕获以避免错误时,它不起作用并且我一直收到相同的错误
重现问题:在 B2 值 a、b、c 列表中创建新工作表并设置验证,拒绝其他
function test() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
try { // don't work !!
ss.getRange('B2').setValue('x'); // value not in validation list
} catch(e) {Logger.log('error')}
}
问题出在哪里???
解决方案
您可以使用flush()
set 之后的方法以到达 catch 部分。
function test() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
try { // don't work !!
ss.getRange('B2').setValue('x'); // a text value not in validation list (with numeric values don´t fail)
SpreadsheetApp.flush()
} catch(e) {Logger.log('error')}
}
推荐阅读
- vb.net - 循环遍历vb中所有目录中的文件
- python - 在 azureml Dataset from_delimited_files 调用中处理丢失文件的最佳方法
- mongodb - 如何在 MongoDB 中的索引字段上加速 $gt 游标?
- angular - Angular - 当子组件仍在加载时父组件卡住
- html - 需要从 XML 属性创建 HTML 表
- javascript - reactjs中如何在EventListener之后仍然定义变量?
- css - 在容器外显示框的样式问题
- php - 如何使用用户可以在 PHP 脚本中编写内容的选项插入和编辑多个复选框?
- amazon-web-services - Terraform 从 aws_launch_configuration 获取 public_ip 的输出
- angular - 带有 Angular 的 Nebular 中的 AWS cognito 刷新令牌问题