将最后一行与 GetRangeByIndexes 结合使用 - Excel
Using Last Row with GetRangeByIndexes - Excel
我正在尝试学习在 JS 中创建范围的最佳实践。我试图避免我在学习 VBA 时犯的错误,即我使用 Range("A1:A2")
格式很长时间才意识到使用 Range(Cells(1,1),Cells(2,1))
更好,因为整数通常是更容易使用。
我发现 getRangeByIndexes
适用于 JS,它并不完美,因为如果第一行不是 0,我必须对行数进行数学运算。我更喜欢使用整数来设置第一个和最后一个范围内的单元格,但那是另一回事了。
目前,如果我在数字中进行硬编码,我可以 select 范围,但现在我正在努力添加“LastRow”函数以使范围动态化,但我无法得到它工作。我也努力让它打印到 console.log,但我决定只尝试使用范围而不是打印出来。我目前正在编辑 taskpane.js
并且对此很陌生。
到目前为止,这是我的代码:
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
/* global console, document, Excel, Office */
// The initialize function must be run each time a new page is loaded
Office.initialize = () => {
document.getElementById("sideload-msg").style.display = "none";
document.getElementById("app-body").style.display = "flex";
document.getElementById("run").onclick = run;
};
export async function run() {
try {
await Excel.run(async (context) => {
var ws = context.workbook.worksheets.getItem("Sheet1");
ws.activate();
var lrow = ws.getUsedRange().getLastRow();
lrow.load("rowindex");
context.sync();
var range = ws.getRangeByIndexes(0, 0, 4, 5); //This Works
//var range = ws.getRangeByIndexes(0, 0, lrow.rowIndex, 5); //This does nothing, but no errors either, just nothing.
range.select();
await context.sync();
console.log("END");
});
} catch (error) {
console.error(error);
}
}
如果我将 lrow.rowindex
替换为 4
,它会按预期工作。谁能告诉我我做错了什么,如果这是生成带数字范围的最佳方法(有没有办法做 first/last 单元格?)
我相信您必须调用 await context.sync() 才能使用您加载的任何属性。您正在尝试在 context.sync() 调用之前使用已加载的 属性 (rowIndex)。所以这就是我认为它不起作用的原因。
如果您从此更新代码:
var lrow = ws.getUsedRange().getLastRow();
lrow.load("rowindex");
var range = ws.getRangeByIndexes(0, 0, 4, 5); //This Works
//var range = ws.getRangeByIndexes(0, 0, lrow.rowIndex, 5); //This does nothing, but no errors either, just nothing.
range.select();
await context.sync();
为此:
var lrow = ws.getUsedRange().getLastRow();
lrow.load("rowindex");
await context.sync();
//var range = ws.getRangeByIndexes(0, 0, 4, 5); //This Works
var range = ws.getRangeByIndexes(0, 0, lrow.rowIndex, 5); //This does nothing, but no errors either, just nothing.
range.select();
await context.sync();
这应该可以解决问题。
您肯定需要在第一个“context.sync()”语句中使用“等待”。我添加了它,只要 sheet 1 上有数据,它就可以正常工作。如果 lrow.rowIndex 计算为 0,它将导致错误,因为 getRangesByIndexes 至少需要一行到 select。
这是我 运行 的代码。
async function run() {
try {
await Excel.run(async (context) => {
var ws = context.workbook.worksheets.getItem("Sheet1");
ws.activate();
var lrow = ws.getUsedRange().getLastRow();
lrow.load("rowindex");
await context.sync();
var range = ws.getRangeByIndexes(0, 0, lrow.rowIndex, 5);
range.select();
await context.sync();
console.log("END");
});
} catch (error) {
console.error(error);
}
}
我接受了“导出”指令,因为我工作的环境抱怨它。
如果您有兴趣,我正在使用加载项商店中名为“JavaScript 自动化开发环境 (JADE)”的加载项。它旨在自动化工作簿中的代码,而不是构建加载项,但它对于测试此类内容非常简单。免责声明:我写了 JADE。
这是与 context.sync
相关的定义。我仍在学习 promise/return/async
等。我切换到 Visual Studio 并从那里的项目文件中得到了更好的主意,这是我对项目文件中任务窗格的默认功能的调整。
注意:嵌套函数完成了函数的其余部分。
function setColor() {
Excel.run(function (context) {
//Start Func
var ws = context.workbook.worksheets.getActiveWorksheet();
var lrow = ws.getUsedRange().getLastRow();
lrow.load("rowindex");
// Run the queued-up command, and return a promise to indicate task completion
return context.sync()
.then(function () {
//var range = ws.getRangeByIndexes(0, 0, 4, 5); //This Works
var range = ws.getRangeByIndexes(0, 0, lrow.rowIndex, 5); //This does nothing, but no errors either, just nothing.
range.select();
})
//End Func
return context.sync();
}).catch(function (error) {
console.log("Error: " + error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});
}
})();
我正在尝试学习在 JS 中创建范围的最佳实践。我试图避免我在学习 VBA 时犯的错误,即我使用 Range("A1:A2")
格式很长时间才意识到使用 Range(Cells(1,1),Cells(2,1))
更好,因为整数通常是更容易使用。
我发现 getRangeByIndexes
适用于 JS,它并不完美,因为如果第一行不是 0,我必须对行数进行数学运算。我更喜欢使用整数来设置第一个和最后一个范围内的单元格,但那是另一回事了。
目前,如果我在数字中进行硬编码,我可以 select 范围,但现在我正在努力添加“LastRow”函数以使范围动态化,但我无法得到它工作。我也努力让它打印到 console.log,但我决定只尝试使用范围而不是打印出来。我目前正在编辑 taskpane.js
并且对此很陌生。
到目前为止,这是我的代码:
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
/* global console, document, Excel, Office */
// The initialize function must be run each time a new page is loaded
Office.initialize = () => {
document.getElementById("sideload-msg").style.display = "none";
document.getElementById("app-body").style.display = "flex";
document.getElementById("run").onclick = run;
};
export async function run() {
try {
await Excel.run(async (context) => {
var ws = context.workbook.worksheets.getItem("Sheet1");
ws.activate();
var lrow = ws.getUsedRange().getLastRow();
lrow.load("rowindex");
context.sync();
var range = ws.getRangeByIndexes(0, 0, 4, 5); //This Works
//var range = ws.getRangeByIndexes(0, 0, lrow.rowIndex, 5); //This does nothing, but no errors either, just nothing.
range.select();
await context.sync();
console.log("END");
});
} catch (error) {
console.error(error);
}
}
如果我将 lrow.rowindex
替换为 4
,它会按预期工作。谁能告诉我我做错了什么,如果这是生成带数字范围的最佳方法(有没有办法做 first/last 单元格?)
我相信您必须调用 await context.sync() 才能使用您加载的任何属性。您正在尝试在 context.sync() 调用之前使用已加载的 属性 (rowIndex)。所以这就是我认为它不起作用的原因。
如果您从此更新代码:
var lrow = ws.getUsedRange().getLastRow();
lrow.load("rowindex");
var range = ws.getRangeByIndexes(0, 0, 4, 5); //This Works
//var range = ws.getRangeByIndexes(0, 0, lrow.rowIndex, 5); //This does nothing, but no errors either, just nothing.
range.select();
await context.sync();
为此:
var lrow = ws.getUsedRange().getLastRow();
lrow.load("rowindex");
await context.sync();
//var range = ws.getRangeByIndexes(0, 0, 4, 5); //This Works
var range = ws.getRangeByIndexes(0, 0, lrow.rowIndex, 5); //This does nothing, but no errors either, just nothing.
range.select();
await context.sync();
这应该可以解决问题。
您肯定需要在第一个“context.sync()”语句中使用“等待”。我添加了它,只要 sheet 1 上有数据,它就可以正常工作。如果 lrow.rowIndex 计算为 0,它将导致错误,因为 getRangesByIndexes 至少需要一行到 select。
这是我 运行 的代码。
async function run() {
try {
await Excel.run(async (context) => {
var ws = context.workbook.worksheets.getItem("Sheet1");
ws.activate();
var lrow = ws.getUsedRange().getLastRow();
lrow.load("rowindex");
await context.sync();
var range = ws.getRangeByIndexes(0, 0, lrow.rowIndex, 5);
range.select();
await context.sync();
console.log("END");
});
} catch (error) {
console.error(error);
}
}
我接受了“导出”指令,因为我工作的环境抱怨它。
如果您有兴趣,我正在使用加载项商店中名为“JavaScript 自动化开发环境 (JADE)”的加载项。它旨在自动化工作簿中的代码,而不是构建加载项,但它对于测试此类内容非常简单。免责声明:我写了 JADE。
这是与 context.sync
相关的定义。我仍在学习 promise/return/async
等。我切换到 Visual Studio 并从那里的项目文件中得到了更好的主意,这是我对项目文件中任务窗格的默认功能的调整。
注意:嵌套函数完成了函数的其余部分。
function setColor() {
Excel.run(function (context) {
//Start Func
var ws = context.workbook.worksheets.getActiveWorksheet();
var lrow = ws.getUsedRange().getLastRow();
lrow.load("rowindex");
// Run the queued-up command, and return a promise to indicate task completion
return context.sync()
.then(function () {
//var range = ws.getRangeByIndexes(0, 0, 4, 5); //This Works
var range = ws.getRangeByIndexes(0, 0, lrow.rowIndex, 5); //This does nothing, but no errors either, just nothing.
range.select();
})
//End Func
return context.sync();
}).catch(function (error) {
console.log("Error: " + error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});
}
})();