【发布时间】:2020-11-15 02:47:41
【问题描述】:
我想要一个附加到电子表格的脚本中的函数,以遍历所有命名范围,并将它们输入到“数据”表的单元格中。此表有很多列表等,我的主表(“分析”)使用这些列表。
理想情况下,这会将它们从第 1001 行开始,其中 col A 是名称,col D 是范围。 (列 B 和 C 留空以允许列 A 溢出 - 不想调整列 A 的大小)
为了有效,函数先前输入的行中的任何现有数据都应首先删除,然后再使用新数据进行更新。
虽然我掌握了 Script 语言的窍门,但我正在为此苦苦挣扎,所以如果有人能提供一个示例函数,我将不胜感激! MTIA。
[编辑] 如果我最初的问题不充分,我深表歉意。我有一些代码,但它是垃圾,我知道它,所以我废弃了它。现在差不多了(我认为),但还有一个问题需要排序,得到一个例外:
function namedRangeToSpreadsheet() {
// Author: Cooper
// Link: https://stackoverflow.com/a/64841828/190925
// Purpose: Record all NamedRanges by both Name and Range, as a check that
// public copy is NOT missing any!
const ss=SpreadsheetApp.getActive();
const nrA=ss.getNamedRanges();
var startRow = 1001; // MH insert from this row
var endRow = 1200; // MH clear|insert to a max. of this
// var targetSh = "Data"; // MH revert to Data ss when tests OK
var targetSh = "Named Ranges";
// MH clear previous data
let sh=ss.getSheetByName(targetSh);
var range = sh.getRange(targetSh + "!A" + startRow + ":D" + endRow);
range.clear();
//sh.getRange(sh.getLastRow()+1,1,rowA.length,4).clear();
let rowA=[];
for(let i=0;i<nrA.length;i++) {
let name=nrA[i].getName();
let range=nrA[i].getRange().getA1Notation();
rowA.push([name,'','',range]);
}
rowA.sort(); // MH
/* Exception: The number of rows in the data does not match the number of rows in the range.
The data has 32 but the range has 200. (line 348, file "Code") */
//sh.getRange(sh.getLastRow()+1,1,rowA.length,4).setValues(rowA);
range.setValues(rowA);
}
【问题讨论】:
-
我无法理解你的目标。我为此道歉。可以问一下
Ideally, this would put them starting at Row 1001, with col A being the Name, and col D being the Range. (Cols B & C left blank to allow for Overflow from col A - don't want to resize col A) To be effective, any existing data in the rows previously entered by the Function should first be deleted, before updating with the new data.的详细信息吗?通过正确理解您的目标,我想考虑解决方案。
标签: google-apps-script google-sheets named-ranges