我正在编写一个程序,对表进行排序,然后拆分表 E 列中的字符串,将 E 列中的值削减到前 10 个字符,并将其余部分放入 F 列中。
function main(workbook: ExcelScript.Workbook) {
let masterA = workbook.getTable("MasterA");
// Sort on table: masterA column index: '8'
masterA.getSort().apply([{key: 8, ascending: true}]);
// Sort on table: masterA column index: '2'
masterA.getSort().apply([{key: 2, ascending: true}]);
let tableRows = masterA.getRowCount();
let sheet = workbook.getActiveWorksheet();
// Set the range to the column of data to modify
let lastRowCount = sheet.getTable("MasterA").getRowCount()
if (lastRowCount > 0) {// Adjust the range to include all rows in the column
let range = sheet.getRangeByIndexes(1, 0, lastRowCount, 10);
let vals = range.getValues();
for (let i=1; i< tableRows; i++) {
let value: string = range.getValue[i][5]
let len: number = value.length
if (len > 10) {
vals[i][6] = value.substring(10)
vals[i][5] = value.substring(0,9)
}
}}
}
当我尝试运行时出现此错误:“第 16 行:无法读取未定义的属性(读取“5”)”参考
let value: string = range.getValue[i][5]
有什么想法吗?这是我第一次尝试 Office 脚本,我可能会遗漏一些明显的东西。
使用
vals[i][5]
获取值,然后 toString()
将其转换为字符串。
for (let i = 1; i < tableRows; i++) {
let value: string = vals[i][5].toString();
let len: number = value.length
if (len > 10) {
vals[i][6] = value.substring(10)
vals[i][5] = value.substring(0, 9)
}