似乎Excel JS-API执行Range.values的浅拷贝。如果我想从一个地方读取范围并将其不同的修改后的副本写入2个不同的地方,我需要使用该范围的深层副本:我该怎么做? 这段代码仍然是一个浅表副本:
async function setValue() {
try {
await Excel.run(async (context) => {
let sheet = context.workbook.worksheets.getActiveWorksheet();
// A1 contains 9876543210
let rng1 = sheet.getRange("a1").load("values");
await context.sync();
console.log(JSON.stringify(rng1.values));
// rng1 value is correct =9876543210
let rng2 = sheet.getRange("B4");
let rng3 = sheet.getRange("B6");
let avar = [[]];
for (var j = 0; j < rng1.values.length; j++) {
for (var k = 0; k < rng1.values[0].length; k++) {
avar[j][k] = rng1.values[j][k];
}
}
rng3.values = avar;
console.log(JSON.stringify(avar));
// rng2 value is correct =9876543210
let avar2 = avar[0][0];
rng3.values[0][0] =avar2 + 0.01;
console.log(JSON.stringify(rng3.values));
// rng3 value is correct =9876543210.01
rng2.values = avar;
console.log("Show values of the 3 ranges before Sync")
console.log(JSON.stringify(rng1.values));
console.log(JSON.stringify(rng2.values));
console.log(JSON.stringify(rng3.values));
// rng2 and rng3 both show as 987654321.01
// BUT ONLY Rng3 has been altered!
await context.sync();
});
console.log("Done!");
}
catch (error) {
OfficeHelpers.Utilities.log(error);
}
}
答案 0 :(得分:0)
要回答我自己的问题 - 您可以使用JQuery扩展
进行深层复制 let avar = [];
$.extend(true, avar, rng1.values);
rng3.values = avar;