应用边框格式
Apply border formatting
我正在尝试设置与新创建的 sheet 关联的一系列单元格的样式。
我最近的 2 次(失败)尝试是:
Excel.run(function (context) {
const newSheet = context.workbook.worksheets.add("New sheet 1");
newSheet.activate();
const values = [
["Row 1", "Row 1", "Row 1"],
["Row 2", "Row 2", "Row 2"]
]
newSheet.getRange("A1").getResizedRange(values.length - 1, values[0].length - 1).values = values;
context.sync().then(function () {
newSheet.getRange("A2").getResizedRange(0, output[0].length - 1).foramt = {borderTopStyle: "thin"};
return context.sync();
});
});
并且:
Excel.run(function (context) {
const newSheet = context.workbook.worksheets.add("New sheet 1");
newSheet.activate();
const values = [
["Row 1", "Row 1", "Row 1"],
["Row 2", "Row 2", "Row 2"]
]
newSheet.getRange("A1").getResizedRange(values.length - 1, values[0].length - 1).values = values;
context.sync().then(function () {
// The difference is here
newSheet.getRange("A2").getResizedRange(0, values[0].length - 1).foramt.borderTopStyle = "thin";
return context.sync();
});
});
为一系列单元格设置样式的正确方法是什么?
首先,关于您发布的代码的整体 structure/contents 的一些 comments/suggestions:
- 一定要始终包含错误处理逻辑,否则事情可能会悄无声息地失败,而您却不知道哪里出了问题。
- 对于您的场景,只需要一个 context.sync()(最后),因为您在此之前所做的事情(即,将数据写入 sheet 并应用格式sheet) 可以简单地排队并在最后的单个 context.sync() 中一起执行。
- 您的代码包含拼写错误(格式而不是格式)并引用了 RangeFormat 对象 (borderTopStyle) 上不存在的 属性 名称。如果您使用 TypeScript 而不是普通 JavaScript,那么系统会自动为您标记此类错误。
下面的代码片段展示了如何为指定范围设置边框(基于您发布的代码的结构,但经过修改以纳入上述反馈):
Excel.run(function (context) {
// create new sheet, add 2 rows of data
const newSheet = context.workbook.worksheets.add("New sheet 1");
newSheet.activate();
const values = [
["Row 1", "Row 1", "Row 1"],
["Row 2", "Row 2", "Row 2"]
]
newSheet.getRange("A1").getResizedRange(values.length - 1, values[0].length - 1).values = values;
// set top border for the second row of data
newSheet.getRange("A2").getResizedRange(0, values[0].length - 1).format.borders.getItem('EdgeTop').style = 'Continuous';
return context.sync();
})
.catch(function (error) {
console.log('error: ' + error);
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
}
我正在尝试设置与新创建的 sheet 关联的一系列单元格的样式。
我最近的 2 次(失败)尝试是:
Excel.run(function (context) {
const newSheet = context.workbook.worksheets.add("New sheet 1");
newSheet.activate();
const values = [
["Row 1", "Row 1", "Row 1"],
["Row 2", "Row 2", "Row 2"]
]
newSheet.getRange("A1").getResizedRange(values.length - 1, values[0].length - 1).values = values;
context.sync().then(function () {
newSheet.getRange("A2").getResizedRange(0, output[0].length - 1).foramt = {borderTopStyle: "thin"};
return context.sync();
});
});
并且:
Excel.run(function (context) {
const newSheet = context.workbook.worksheets.add("New sheet 1");
newSheet.activate();
const values = [
["Row 1", "Row 1", "Row 1"],
["Row 2", "Row 2", "Row 2"]
]
newSheet.getRange("A1").getResizedRange(values.length - 1, values[0].length - 1).values = values;
context.sync().then(function () {
// The difference is here
newSheet.getRange("A2").getResizedRange(0, values[0].length - 1).foramt.borderTopStyle = "thin";
return context.sync();
});
});
为一系列单元格设置样式的正确方法是什么?
首先,关于您发布的代码的整体 structure/contents 的一些 comments/suggestions:
- 一定要始终包含错误处理逻辑,否则事情可能会悄无声息地失败,而您却不知道哪里出了问题。
- 对于您的场景,只需要一个 context.sync()(最后),因为您在此之前所做的事情(即,将数据写入 sheet 并应用格式sheet) 可以简单地排队并在最后的单个 context.sync() 中一起执行。
- 您的代码包含拼写错误(格式而不是格式)并引用了 RangeFormat 对象 (borderTopStyle) 上不存在的 属性 名称。如果您使用 TypeScript 而不是普通 JavaScript,那么系统会自动为您标记此类错误。
下面的代码片段展示了如何为指定范围设置边框(基于您发布的代码的结构,但经过修改以纳入上述反馈):
Excel.run(function (context) {
// create new sheet, add 2 rows of data
const newSheet = context.workbook.worksheets.add("New sheet 1");
newSheet.activate();
const values = [
["Row 1", "Row 1", "Row 1"],
["Row 2", "Row 2", "Row 2"]
]
newSheet.getRange("A1").getResizedRange(values.length - 1, values[0].length - 1).values = values;
// set top border for the second row of data
newSheet.getRange("A2").getResizedRange(0, values[0].length - 1).format.borders.getItem('EdgeTop').style = 'Continuous';
return context.sync();
})
.catch(function (error) {
console.log('error: ' + error);
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
}