我是谷歌脚本的新手,遇到了一些我无法弄清楚的事情。我试图命名一个范围,希望最终将其放入一个数组并用数据填充另一张表。
我在命名范围时遇到了问题。我希望最终能够遍历并命名许多范围,因此我希望能够将变量用于行号。
如果我试试这个:
var rngProject = "A8:Y21";
var RngName = "project1";
然后一切正常,我有一个数组中的数据。但是,如前所述,我想为行号以及范围名称使用变量,如下所示。
如果我试试这个:
var rngProject = "A" + strtRow + ":Y" + totRows + strtRow - 1;
var RngName = "project" + toString(i);
在这条线上:
var rng_to_name = active_spreadsheet.getSheetByName(sheet_name).getRange(address);
我收到错误消息:“参数必须是一个范围。” 在调试器中,地址值显示为 NaN,所以我认为这是不对的。但是我也尝试过使用 strRange (在下面注释掉),我在同一行得到了同样的错误。
有没有办法可以获取这个值并将其转换为范围类型,或者我做错了什么?
function namePrjRange () {
var i = 1
var strtRow = 8;
var shtName = "USER INPUT by week: TEST";
var totRows = 14;
//var strRange = "A" + toString(strtRow) + ":Y" + toString(totRows + strtRow - 1);
var rngProject = "A" + strtRow + ":Y" + totRows + strtRow - 1;
var RngName = "project" + toString(i);
//Logger.log(strRange);
Logger.log(rngProject);
Logger.log(RngName);
nameRange(shtName, rngProject, RngName);
}
// Given a sheet name (assumed to be in the active spreadsheet, an address using "A1" notation,
// and a name string, assign that name to the range address in the given sheet.
function nameRange(sheet_name, address, rng_name) {
// Create an spreadsheet object.
Logger.log("Entered nameRange function")
var active_spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// Get a range object by firstly getting a sheet object from the spreadsheet object and then pass the
// "address" argument to the sheet "getRange()" method.
var rng_to_name = active_spreadsheet.getSheetByName(sheet_name).getRange(address);
// Use the spreadsheet object method "setNamedRange()" to assign a range name to a range object.
Logger.log("About to set Named Range");
Logger.log(rng_name);
Logger.log(rng_to_name);
active_spreadsheet.setNamedRange(rng_name, rng_to_name);
}