Skip to content

Instantly share code, notes, and snippets.

@ShellyShulei
Created December 9, 2019 15:34
Show Gist options
  • Select an option

  • Save ShellyShulei/0eedbb61544250bd11ce6b8224658241 to your computer and use it in GitHub Desktop.

Select an option

Save ShellyShulei/0eedbb61544250bd11ce6b8224658241 to your computer and use it in GitHub Desktop.
Import JSON data into a table
name: Import JSON data
description: Import JSON data into a table
host: EXCEL
api_set: {}
script:
content: |
$("#import-json-data").click(importJsonData);
async function importJsonData() {
try {
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:C1", true);
expensesTable.name = "ExpensesTable";
expensesTable.getHeaderRowRange().values = [["IataCode", "IcaoCode", "Name"]];
const newData = transactions.map((item) => [item.IataCode, item.IcaoCode, item.Name]);
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();
});
} catch (error) {
OfficeHelpers.Utilities.log(error);
}
}
const transactions = [
{
IcaoCode: "KSFO",
Name: "San Francisco International Airport",
IataCode: "SFO",
},
{
IcaoCode: "KLAX",
Name: "Los Angeles International Airport",
IataCode: "LAX"
},
{
IcaoCode: "ZSSS",
Name: "Shanghai Hongqiao International Airport",
IataCode: "SHA"
}
];
language: typescript
template:
content: |+
<section class="ms-font-m">
<p>This sample shows how to import json data into a new table the Excel JavaScript API.</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 {\r\n margin-top: 20px;\r\n}\r\n\r\nsection.samples .ms-Button, section.setup .ms-Button {\r\n display: block;\r\n margin-bottom: 5px;\r\n margin-left: 20px;\r\n min-width: 80px;\r\n}\r\n"
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.6.5/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/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