Excel JavaScript API Addin 学习笔记(一)

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版本!

要是有帮助到你,点个赞吧!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我也秃了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值