Office-js-helper

时间:2018-06-24 18:07:06

标签: office-js office-js-helpers

首先,我不是程序员,但从vba到Office js的过程中是一名优秀的业余爱好者,这使我成为了真正的软件制造商。所以也许我的问题很愚蠢。我正在建立一个带有设置的Office添加:

  • Visual Studio社区2017;
  • Office 365企业版E3开发人员;
  • 阅读《使用Office.js构建Office外接程序》一书(michael Zlatkovsky)
  • 用于Office加载项的内置构建模板:Excel加载项功能;
  • 遵循制作项目打字稿的步骤
  • 从结构用户界面切换到引导程序;

脚本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:找不到名称'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();
}

})();

Picture of project solution explore

1 个答案:

答案 0 :(得分:0)

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