Created
February 6, 2019 20:41
-
-
Save dmahugh/15db8c3a9037cb1189fbfce6a936be46 to your computer and use it in GitHub Desktop.
Imports JSON data into a table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Import JSON data | |
description: Imports JSON data into a table. | |
host: EXCEL | |
api_set: {} | |
script: | |
content: | | |
$("#import-json-data").click(() => tryCatch(importJsonData)); | |
async function importJsonData() { | |
await Excel.run(async (context) => { | |
await OfficeHelpers.ExcelUtilities.forceCreateSheet(context.workbook, "Sample"); | |
const sheet = context.workbook.worksheets.getItem("Sample"); | |
let expensesTable = sheet.tables.add("A1:D1", true); | |
expensesTable.name = "ExpensesTable"; | |
expensesTable.getHeaderRowRange().values = [["Date", "Merchant", "Category", "Amount"]]; | |
const newData = transactions.map((item) => [item.DATE, item.MERCHANT, item.CATEGORY, item.AMOUNT]); | |
expensesTable.rows.add(null, newData); | |
if (Office.context.requirements.isSetSupported("ExcelApi", 1.2)) { | |
sheet.getUsedRange().format.autofitColumns(); | |
sheet.getUsedRange().format.autofitRows(); | |
} | |
sheet.activate(); | |
await context.sync(); | |
}); | |
} | |
const transactions = [ | |
{ | |
DATE: "1/1/2017", | |
MERCHANT: "The Phone Company", | |
CATEGORY: "Communications", | |
AMOUNT: "$120" | |
}, | |
{ | |
DATE: "1/1/2017", | |
MERCHANT: "Southridge Video", | |
CATEGORY: "Entertainment", | |
AMOUNT: "$40" | |
}, | |
{ | |
DATE: "1/1/2017", | |
MERCHANT: "Coho Winery", | |
CATEGORY: "Restaurant", | |
AMOUNT: "$47" | |
}, | |
{ | |
DATE: "1/2/2017", | |
TEST: "TEST", | |
CATEGORY: "Shopping", | |
AMOUNT: "$56" | |
}, | |
{ | |
DATE: "1/2/2017", | |
CATEGORY: "Shopping", | |
AMOUNT: "$110" | |
}, | |
{ | |
DATE: "1/2/2017", | |
MERCHANT: "Liberty Bakery & Cafe", | |
CATEGORY: "Groceries", | |
AMOUNT: "$27" | |
}, | |
{ | |
DATE: "1/2/2017", | |
MERCHANT: "Liberty Bakery & Cafe", | |
CATEGORY: "Groceries", | |
AMOUNT: "$38" | |
}, | |
{ | |
DATE: "1/2/2017", | |
MERCHANT: "Northwind Electric Cars", | |
CATEGORY: "Transportation", | |
AMOUNT: "$42" | |
}, | |
{ | |
DATE: "1/2/2017", | |
MERCHANT: "Best For You Organics Company", | |
CATEGORY: "Groceries", | |
AMOUNT: "$27" | |
}, | |
{ | |
DATE: "1/3/2017", | |
MERCHANT: "Contoso, LTD", | |
CATEGORY: "Shopping", | |
AMOUNT: "$25" | |
}, | |
{ | |
DATE: "1/5/2017", | |
MERCHANT: "Munson's Pickles & Preserves Farm", | |
CATEGORY: "Groceries", | |
AMOUNT: "$178" | |
} | |
]; | |
/** Default helper for invoking an action and handling errors. */ | |
async function tryCatch(callback) { | |
try { | |
await callback(); | |
} catch (error) { | |
OfficeHelpers.UI.notify(error); | |
OfficeHelpers.Utilities.log(error); | |
} | |
} | |
language: typescript | |
template: | |
content: |+ | |
<section class="ms-font-m"> | |
<p>This sample shows how to import json data into a new table.</p> | |
</section> | |
<section class="samples ms-font-m"> | |
<h3>Try it out</h3> | |
<button id="import-json-data" class="ms-Button"> | |
<span class="ms-Button-label">Import JSON data</span> | |
</button> | |
</section> | |
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: | | |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js | |
@types/office-js | |
[email protected]/dist/css/fabric.min.css | |
[email protected]/dist/css/fabric.components.min.css | |
[email protected]/client/core.min.js | |
@types/core-js | |
@microsoft/[email protected]/dist/office.helpers.min.js | |
@microsoft/[email protected]/dist/office.helpers.d.ts | |
[email protected] | |
@types/[email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment