1.Create the add-in
刚开始的安装部分没啥好说的
按照官网教程,一步一步来就好
我用的是Yeoman generator 生成,Excel 2016
然后用VS Code编辑
点击Run之后,所选内容变为黄色,也就代表成功了
2.Excel add-in tutorial
进入文档,开始创建第一个table!
Office.onReady部分有几个要注意的地方:
1.function createTable要写在onReady内部,我之前直接添加在最后面,按钮一点反应都没有
2.按照教程上的写法,会只创建表头,没有数据,但你只加第一条数据,就会发现有数据了
3.当发现只有这个图标的时候不要慌,左右拖动一下界面,就好了
没办法,不太会,只能先这样一条一条加了
/*
* 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 */
Office.onReady(info => {
if (info.host === Office.HostType.Excel) {
//The first part of this code determines whether the user's version of Excel supports a version of Excel.js
// that includes all the APIs that this series of tutorials will use. In a production add-in,
// use the body of the conditional block to hide or disable the UI that would call unsupported APIs.
// This will enable the user to still make use of the parts of the add-in that are supported by their version of Excel.
// Determine if the user's version of Office supports all the Office.js APIs that are used in the tutorial.
if (!Office.context.requirements.isSetSupported('ExcelApi', '1.7')) {
console.log('Sorry. The tutorial add-in uses Excel.js APIs that are not available in your version of Office.');
}
document.getElementById("create-table").onclick = createTable;
document.getElementById("sideload-msg").style.display = "none";
document.getElementById("app-body").style.display = "flex";
// Assign event handlers and other initialization logic.
function createTable() {
Excel.run(function (context) {
var currentWorksheet = context.workbook.worksheets.getActiveWorksheet();
var expensesTable = currentWorksheet.tables.add("A1:D1", true /*hasHeaders*/);
expensesTable.name = "ExpensesTable";
expensesTable.getHeaderRowRange().values =
[["Date", "Merchant", "Category", "Amount"]];
expensesTable.rows.add(null /*add at the end*/, [
["1/1/2017", "The Phone Company", "Communications", "120"],
]);
expensesTable.rows.add(null /*add at the end*/, [
["1/2/2017", "Northwind Electric Cars", "Transportation", "142.33"],
]);
expensesTable.rows.add(null /*add at the end*/, [
["1/5/2017", "Best For You Organics Company", "Groceries", "27.9"],
]);
expensesTable.rows.add(null /*add at the end*/, [
["1/10/2017", "Coho Vineyard", "Restaurant", "33"],
]);
expensesTable.rows.add(null /*add at the end*/, [
["1/11/2017", "Bellows College", "Education", "350.1"],
]);
expensesTable.rows.add(null /*add at the end*/, [
["1/15/2017", "Trey Research", "Other", "135"],
]);
expensesTable.rows.add(null /*add at the end*/, [
["1/15/2017", "Best For You Organics Company", "Groceries", "97.88"]
]);
expensesTable.columns.getItemAt(3).getRange().numberFormat = [['\u20AC#,##0.00']];
expensesTable.getRange().format.autofitColumns();
expensesTable.getRange().format.autofitRows();
return context.sync();
})
.catch(function (error) {
console.log("Error: " + error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});
}
}
});
运行完,发现数据添加上了,原因就是Excel2016仅支持 API 1.1版本,而 API 1.4版本之后才有的多行插入,所以教程也要配合着1.1版本食用,或者更新你的Excel版本!
要是有帮助到你,点个赞吧!!!