如何处理Google表格中的所有命名范围?

时间:2015-04-14 18:54:47

标签: google-apps-script google-sheets

有没有办法处理Google表格中的所有命名范围?

Google文档已.getNamedRanges(),但我找不到任何类似的表单。我想阅读并打印每个范围参数。

我的最终目标是整体修复很多命名范围。有问题的文件中有200多个。

表格中存在一个错误(Documented here),因为命名范围中的默认范围具有合并单元格的整个范围(即:A1:J1),但命名范围仅适用于公式你指定JUST是第一个单元格,我以为我可以通过接受默认的命名范围(A1:J1作为示例)来节省大量时间,然后编写脚本以便我可以将所有内容从最后删除,只留下A1。为了做到这一点,我需要简单地获得表格中所有命名范围的列表。

这可能吗?

2 个答案:

答案 0 :(得分:2)

不幸的是,简短的答案是否定的。

在以下位置有一项增强请求:

https://code.google.com/p/google-apps-script-issues/issues/detail?id=917

你可以加注星标投票并接收更新。

答案 1 :(得分:2)

The answer is now, at least, Yes. There remain some frustrating limitations with programmatic handling of named ranges, but the situation is much improved.

The same .getNamedRanges() method cited in the question works when applied to either a Sheet (only the named ranges referencing a range in the sheet) or Spreadsheet (all named ranges in the spreadsheet) object. It returns an array of all Named Ranges.

You cannot access a Named Range directly by name in GAS. A rather maddening oversight requiring iterating through an entire array of named ranges to get to the one needed.

In the end, however, there really isn't much you can't do with them. As several other questions on SO highlight, it's rarely if ever a good idea to programmatically delete a named range. Even though it would be a lot easier to delete one and then re-add it sometimes, that will break every reference in your sheet to the named range (unless they use the awkward indirect("named_range_name") construct).