Skip to content

Instantly share code, notes, and snippets.

@deinspanjer
Created January 28, 2018 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deinspanjer/70d3d62d32d4fe8f34279ab240f7b110 to your computer and use it in GitHub Desktop.
Save deinspanjer/70d3d62d32d4fe8f34279ab240f7b110 to your computer and use it in GitHub Desktop.
Creates a table with four columns and seven rows. - Shared with Script Lab
name: Create a table
description: Creates a table with four columns and seven rows.
author: deinspanjer
host: EXCEL
api_set: {}
script:
content: |
$("#create-table").click(createTable);
$("#delete-rows").click(deleteRows);
async function createTable() {
try {
await Excel.run(async (context) => {
await OfficeHelpers.ExcelUtilities.forceCreateSheet(context.workbook, "Sample");
const sheet = context.workbook.worksheets.getItem("Sample");
const expensesTable = sheet.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"],
["1/2/2017", "Northwind Electric Cars", "Transportation", "$142"],
["1/5/2017", "Best For You Organics Company", "Groceries", "$27"],
["1/10/2017", "Coho Vineyard", "Restaurant", "$33"],
["1/11/2017", "Bellows College", "Education", "$350"],
["1/15/2017", "Trey Research", "Other", "$135"],
["1/15/2017", "Best For You Organics Company", "Groceries", "$97"]
]);
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.Utilities.log(error);
}
}
async function deleteRows() {
try {
await Excel.run(async (context) => {
const expensesTable = context.workbook.tables.getItem("ExpensesTable")
const bodyRows = expensesTable.getDataBodyRange().load("rowCount");
context.sync().then(() => {
if (bodyRows.rowCount == 7) {
const lastThreeRows = bodyRows.getOffsetRange(4, 0).getResizedRange(-4, 0);
// 1. Why is this needed?
// 2. Why shouldn't it have await as a prefix?
context.sync();
lastThreeRows.delete(null);
}
});
});
}
catch (error) {
OfficeHelpers.UI.notify(error);
OfficeHelpers.Utilities.log(error);
}
}
language: typescript
template:
content: "<section class=\"ms-font-m\">\n <p>This sample shows how to create a table using the Excel JavaScript API.</p>\n</section>\n\n<section class=\"samples ms-font-m\">\n <h3>Try it out</h3>\n <button id=\"create-table\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Create table</span>\n </button>\n <br/>\n\t<button id=\"delete-rows\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Delete a few rows</span>\n </button></section>\n"
language: html
style:
content: |
section.samples {
margin-top: 20px;
}
section.samples .ms-Button, section.setup .ms-Button {
display: block;
margin-bottom: 5px;
margin-left: 20px;
min-width: 80px;
}
language: css
libraries: |
// Office.js
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
// CSS Libraries
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css
// NPM libraries
core-js@2.4.1/client/core.min.js
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.min.js
jquery@3.1.1
// IntelliSense: @types/library or node_modules paths or URL to d.ts files
@types/office-js
@types/core-js
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.d.ts
@types/jquery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment