-1

首先,我不是程序员,而是我从 vba 到 office js 添加成为真正的软件制造商的过程中的 excel 爱好者。所以也许我的问题很愚蠢。我正在建立一个办公室添加设置:

  • Visual Studio 社区 2017;
  • Office 365 企业版 E3 开发者;
  • 阅读《使用 Office.js 构建 Office 插件》一书 (michael Zlatkovsky)
  • 用于 Office 插件的内置模板:Excel 插件功能;
  • 按照步骤制作项目打字稿
  • 从fabric ui切换到bootstrap;

在 script Lab 中有很酷的示例代码片段,它使用了 office-js-helper 库 (github)。对于问题部分

async function createTable() {
    try {
        await Excel.run(async (context) => {

            await OfficeHelpers.ExcelUtilities.forceCreateSheet(context.workbook, "Indeling");

书中还提到了officehelper,但没有描述如何在VS的标准模板中实现它。它是一个 npm 库。https://github.com/OfficeDev/office-js-helpers/blob/master/README.md 我试图在 html 的头部引用脚本,但没有正确。我尝试通过快速安装包添加它(在解决方案资源管理器中单击鼠标右键)。这个库还有一个 Nuget 变体吗?在构建时出现错误 Build:Cannot find name 'OfficeHelpers'(TS2304)。

如何做到这一点?

编辑

<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<title>Grip op mijn Geld Develop</title>

<script src="office.helpers.min.js"></script>
<script src="https://unpkg.com/@microsoft/office-js-helpers@1.0.2/dist/office.helpers.min.js"></script>

<script src="Scripts/jquery-3.3.1.js"></script>
<script src="Scripts/FabricUI/MessageBanner.js" type="text/javascript"></script>
<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>

    <!-- To enable offline debugging using a local reference to Office.js, use:                        -->
<!-- <script src="Scripts/Office/MicrosoftAjax.js" type="text/javascript"></script>  -->
<!-- <script src="Scripts/Office/1/office.js" type="text/javascript"></script>  -->

<link href="Home.css" rel="stylesheet" type="text/css" />
<script src="Home.js" type="text/javascript"></script>
<script src="Home.js"></script>
<!--<script src="https://unpkg.com/@microsoft/office-js-helpers@1.0.2/dist/office.helpers.min.js"></script>-->
<!-- For the Office UI Fabric, go to https://aka.ms/office-ui-fabric to learn more.
<link rel="stylesheet" href="https://appsforoffice.microsoft.com/fabric/2.1.0/fabric.min.css">
<link rel="stylesheet" href="https://appsforoffice.microsoft.com/fabric/2.1.0/fabric.components.min.css">-->
<!-- To enable the offline use of Office UI Fabric, use: -->
<link rel="stylesheet" href="Content/fabric.min.css">
<link rel="stylesheet" href="Content/fabric.components.min.css">


<!-- For the Bootstrap UI -->
<link href="Content/bootstrap-grid.css" rel="stylesheet" />
<link href="Content/bootstrap-reboot.css" rel="stylesheet" />
<link href="Content/bootstrap.css" rel="stylesheet" />
<link href="Content/fontawesome.css" rel="stylesheet" />
<link href="Content/fontawesome-all.css" rel="stylesheet" />
<link href="Content/OverrideCss.css" rel="stylesheet" />

和 home.ts

declare var fabric: any;

(function () {
"use strict";

var cellToHighlight;
var messageBanner;

// The initialize function must be run each time a new page is loaded.
Office.initialize = function (reason) {
    $(document).ready(function () {
        // Initialize the FabricUI notification mechanism and hide it
        var element = document.querySelector('.ms-MessageBanner');
        messageBanner = new fabric.MessageBanner(element);
        messageBanner.hideBanner();

        // If not using Excel 2016, use fallback logic.
        if (!Office.context.requirements.isSetSupported('ExcelApi', 1.1)) {

            return;
        }

        // Attach button click event handlers
        $("#create-table").click(createTable);
        $("#create-table2").click(createTable);


        (window as any).Promise = OfficeExtension.Promise   

    });
};
async function createTable2() {
    try {
        await Excel.run(async (context) => {

            // await OfficeHelpers.ExcelUtilities.forceCreateSheet(context.workbook, "Indeling");

            await OfficeHelpers.ExcelUtilities.forceCreatesheet(context.workbook, "Sample");

            const sheet = context.workbook.worksheets.getItem("sample");

            const expenseTable = sheet.tables.add("A1:d1",true)

            expenseTable.getHeaderRowRange().values = [["Type", "Soort", "Volgnr", "Hulp-Sorteer"]]

            expenseTable.rows.add(null /*add at the end*/, [
                ["1/1/2018", "the phone comp", "Communications", " €120"],
                ["1/5/2018", "the phone comp", "Anders", " €150"],
            ]);

            if (Office.context.requirements.isSetSupported('ExcelApi', 1.2)) {
                sheet.getUsedRange().format.autofitColumns();
                sheet.getUsedRange().format.autofitRows();
            }

            sheet.activate();

            await context.sync();


        });

    }
    catch (error) {
        OfficeHelpers.UI.notify(error);
        OfficeHelpers.Uilities.log(error);
    }



}




async function createTable() {
    try {
        await Excel.run(async (context) => {

            // await OfficeHelpers.ExcelUtilities.forceCreateSheet(context.workbook, "Indeling");

            let sheets = context.workbook.worksheets;
            let sheetIndeling = sheets.add("Indeling");
            sheetIndeling.load("Indeling");

            // Create strings to store all static content to display in the Indeling sheet
            let sheetTitle = "Indeling";
            let sheetSubTitel1 = "Herkennen";
            let sheetSubTitel2 = "Categorieën";
            let sheetSubTitel3 = "Soort";
            let table1 = "HerkennenTable";
            let table2 = "CategorieTable";
            let table3 = "SoortTable";

            //Queue a command to fill white color in the sheet to remove gridlines
            sheetIndeling.getRange().format.fill.color = "white";

            // Add all static content to the Welcome sheet and format the text
            addContentToWorksheet(sheetIndeling, "B1:N1", sheetTitle, "SheetTitle");
            addContentToWorksheet(sheetIndeling, "B3:E3", sheetSubTitel1, "SheetSubTitle");
            addContentToWorksheet(sheetIndeling, "G3:I3", sheetSubTitel2, "SheetSubTitle");
            addContentToWorksheet(sheetIndeling, "K3:N3", sheetSubTitel3, "SheetSubTitle");
            addContentToWorksheet(sheetIndeling, "B5:E5", table1, "TableHeading");
            addContentToWorksheet(sheetIndeling, "G5:I5", table2, "TableHeading");
            addContentToWorksheet(sheetIndeling, "K5:N5", table3, "TableHeading");

            const sheet = context.workbook.worksheets.getItem("Indeling");
            const herkennenTable = sheet.tables.add("B5:E5", true);
            const categorieTable = sheet.tables.add("G5:I5", true);
            const soortTable = sheet.tables.add("K5:N5", true);


            // Queue a command to set the header row
            herkennenTable.getHeaderRowRange().values = [["Soort", "Categorie", "Herkennen-Kenmerk", "Nr"]];
            categorieTable.getHeaderRowRange().values = [["Soort", "Categorie", "ID"]];
            soortTable.getHeaderRowRange().values = [["Type","Soort","Volgnr","Hulp-Sorteer"]]

            herkennenTable.rows.add(null /*add at the end*/, [
                ["1/1/2018", "the phone comp", "Communications"," €120"],
                ["1/5/2018", "the phone comp", "Anders", " €150"],
            ]);

            if (Office.context.requirements.isSetSupported('ExcelApi', 1.2)) {
                sheet.getUsedRange().format.autofitColumns();
                sheet.getUsedRange().format.autofitRows();
            }

            sheet.activate();

            await context.sync();


        });

    }
    catch (errorHandler) {

    }



}
// Helper function to add and format content in the workbook
function addContentToWorksheet(sheetObject, rangeAddress, displayText, typeOfText) {

    // Format differently by the type of content
    switch (typeOfText) {
        case "SheetTitle":
            var range = sheetObject.getRange(rangeAddress);
            range.values = displayText;
            range.format.font.name = "Corbel";
            range.format.font.size = 24;
            range.format.font.color = "yellow";
            range.merge();
            //Fill color in the brand bar
            sheetObject.getRange("A1:M1").format.fill.color = "#1f4e78";
            break;
        case "SheetSubTitle":
            var range = sheetObject.getRange(rangeAddress);
            range.values = displayText;
            range.format.font.name = "Corbel";
            range.format.font.size = 14;
            range.format.font.color = "#008000";
            range.format.fill.color = "yellow";
            range.format.horizontalAlignment = "center";
            range.merge();
            break;
        case "SheetHeading":
            var range = sheetObject.getRange(rangeAddress);
            range.values = displayText;
            range.format.font.name = "Corbel";
            range.format.font.size = 18;
            range.format.font.color = "#1f4e78";
            range.merge();
            break;
        case "SheetHeadingDesc":
            var range = sheetObject.getRange(rangeAddress);
            range.values = displayText;
            range.format.font.name = "Corbel";
            range.format.font.size = 10;
            range.merge();
            break;
        case "SummaryDataHeader":
            var range = sheetObject.getRange(rangeAddress);
            range.values = displayText;
            range.format.font.name = "Corbel";
            range.format.font.size = 13;
            range.merge();
            break;
        case "SummaryDataValue":
            var range = sheetObject.getRange(rangeAddress);
            range.numberFormat = "$#";
            range.values = displayText;
            range.format.font.name = "Corbel";
            range.format.font.size = 13;
            range.merge();
            break;
        case "TableHeading":
            var range = sheetObject.getRange(rangeAddress);
            range.values = displayText;
            range.format.font.name = "Corbel";
            range.format.font.size = 12;
            range.format.fill.color = "#1f4e78"
            range.format.font.color = "yellow";
            range.merge();
            break;
        case "TableHeaderRow":
            var range = sheetObject.getRange(rangeAddress);
            range.format.font.name = "Corbel";
            range.format.font.size = 10;
            range.format.font.bold = true;
            range.format.font.color = "black";
            break;
        case "TableDataRows":
            var range = sheetObject.getRange(rangeAddress);
            range.format.font.name = "Corbel";
            range.format.font.size = 10;
            sheetObject.getRange(rangeAddress).format.borders.getItem('EdgeBottom').style = 'Continuous';
            sheetObject.getRange(rangeAddress).format.borders.getItem('EdgeTop').style = 'Continuous';
            break;
        case "TableTotalsRow":
            var range = sheetObject.getRange(rangeAddress);
            range.format.font.name = "Corbel";
            range.format.font.size = 10;
            range.format.font.bold = true;
            break;
    }
}




// Helper function for treating errors
function errorHandler(error) {
    // Always be sure to catch any accumulated errors that bubble up from the Excel.run execution
    showNotification("Error", error);
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
}

// Helper function for displaying notifications
function showNotification(header, content) {
    $("#notification-header").text(header);
    $("#notification-body").text(content);
    messageBanner.showBanner();
    messageBanner.toggleExpansion();
}

})();

项目解决方案探索图片

4

1 回答 1

0

您应该能够通过<script src="https://unpkg.com/@microsoft/office-js-helpers@1.0.2/dist/office.helpers.min.js"></script>HTML 引用该库以进行测试。您还可以在本地复制相同的文件并从那里使用它。那不适合你吗?

于 2018-06-26T06:58:13.303 回答