TypeError: Range is not a function In Google Apps Script
TypeError: Range is not a function In Google Apps Script
我正在尝试编写一个脚本,该脚本将 a) 从 change_log sheet 中提取句柄和字段,然后 B) 使用这些变量查找句柄存在的行, 和该字段存在的列(在同一行),在不同的sheet('Master spreadsheet'),然后C)输出B部分确定的行号和列号。
看来我已经达到了 objective A,但我 运行 在 B 部分遇到了这个错误(错误发生在第 12 行):
Error
TypeError: Range is not a function
changeUpdater @ Code.gs:12
这是我目前写的函数:
function changeUpdater() {
let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('change_log');
let handle = sheet.getRange("E4");
handle = handle.toString();
var field = sheet.getRange("I4");
field = field.toString().substring(6, 41);
sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Master spreadsheet');
let column = 4;
let row = 1;
let columnValues = sheet.getRange(2, column, sheet.getLastRow()).getValues();
let rowValues = sheet.getRange(1, row, sheet.getLastColumn()).getValues();
let index = columnValues.findIndex(handle);
let specColumn = rowValues.findIndex(field);
console.log(index);
console.log(specColumn);
}
感谢任何帮助,我知道这可能是一个新手级别的问题:)
根据JS docs:
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
所以您需要将测试函数名称或 lambda 传递给您的 findIndex
调用。你正在传递一个 Range
实例。
let index = columnValues.findIndex(index => index === handle);
我正在尝试编写一个脚本,该脚本将 a) 从 change_log sheet 中提取句柄和字段,然后 B) 使用这些变量查找句柄存在的行, 和该字段存在的列(在同一行),在不同的sheet('Master spreadsheet'),然后C)输出B部分确定的行号和列号。
看来我已经达到了 objective A,但我 运行 在 B 部分遇到了这个错误(错误发生在第 12 行):
Error
TypeError: Range is not a function
changeUpdater @ Code.gs:12
这是我目前写的函数:
function changeUpdater() {
let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('change_log');
let handle = sheet.getRange("E4");
handle = handle.toString();
var field = sheet.getRange("I4");
field = field.toString().substring(6, 41);
sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Master spreadsheet');
let column = 4;
let row = 1;
let columnValues = sheet.getRange(2, column, sheet.getLastRow()).getValues();
let rowValues = sheet.getRange(1, row, sheet.getLastColumn()).getValues();
let index = columnValues.findIndex(handle);
let specColumn = rowValues.findIndex(field);
console.log(index);
console.log(specColumn);
}
感谢任何帮助,我知道这可能是一个新手级别的问题:)
根据JS docs:
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
所以您需要将测试函数名称或 lambda 传递给您的 findIndex
调用。你正在传递一个 Range
实例。
let index = columnValues.findIndex(index => index === handle);