Skip to content

Instantly share code, notes, and snippets.

@smozgur
Created December 13, 2021 16:19
Show Gist options
  • Save smozgur/bf5276622820b1fbbec6690c1689762c to your computer and use it in GitHub Desktop.
Save smozgur/bf5276622820b1fbbec6690c1689762c to your computer and use it in GitHub Desktop.
How to create your own data type and set the cell values by using Range.valuesAsJson property. ScriptLab demo.
name: Excel Custom Data Type Add-in Demo
description: >-
How to create your own data type and set the cell values by using
Range.valuesAsJson property. ScriptLab demo.
host: EXCEL
api_set: {}
script:
content: |
$("#run").click(() => tryCatch(run));
async function run() {
await Excel.run(async (context) => {
// Get the keyword from the active cell
const fnd = context.workbook.getActiveCell();
fnd.load("values");
await context.sync();
var keyword = fnd.values[0][0];
// Starting from here, it is exactly the same code with the Custom Function
var lookin = "title"; // or body to search in the product article body
keyword = "?" + lookin + "=" + keyword;
const url = "https://demo.batcoder.com/api/book/" + keyword;
const response = await fetch(url);
if (!response.ok) {
throw new Error(response.statusText);
}
const books = await response.json();
var bookEntities = [];
for (var i = 0; i < books.length; i++) {
var book = books[i];
var authors = [];
for (var j = 0; j < book.authors.length; j++) {
var author = book.authors[j];
var authorBooks = [];
for (var k = 0; k < author.books.length; k++) {
var authorBook = author.books[k];
var authorBookEntity = {
type: "String",
basicValue: authorBook
};
authorBooks.push([authorBookEntity]);
}
var authorEntity = {
type: "Entity",
text: author.name,
properties: {
Name: {
type: "String",
basicValue: author.name
},
WebPage: {
type: "String",
basicValue: author.url
},
Website: {
type: "String",
basicValue: author.website
},
Image: {
type: "WebImage",
address: author.image
},
Books: {
type: "Array",
elements: authorBooks
}
}
};
authors.push([authorEntity]);
}
var bookEditions = [];
for (var m = 0; m < book.editions.length; m++) {
var bookEdition = book.editions[m];
var bookEditionEntity = {
type: "String",
basicValue: bookEdition
};
bookEditions.push([bookEditionEntity]);
}
var entity = {
type: "Entity",
text: book.title,
properties: {
Title: {
type: "String",
basicValue: book.title
},
PublishDate: {
type: "FormattedNumber",
basicValue: book.published_on_numeric,
numberFormat: "mm/dd/YYYY"
},
ISBN: {
type: "String",
basicValue: book.isbn
},
URL: {
type: "String",
basicValue: book.url
},
CoverImage: {
type: "WebImage",
address: book.image
},
Authors: {
type: "Array",
elements: authors
}
}
};
if (bookEditions.length > 0) {
entity.properties["Editions"] = {
type: "Array",
elements: bookEditions
};
}
bookEntities.push([entity]);
}
// The following is the addition to the Custom Function code instead of returning the entity
if (bookEntities.length > 0) {
// Prepare the target range starting from the search cell
var rng = fnd.getResizedRange(bookEntities.length - 1, 0);
rng.valuesAsJson = bookEntities;
} else {
console.log("no books");
}
// Done!
await context.sync();
});
}
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
console.error(error);
}
}
language: typescript
template:
content: |
<button id="run" class="ms-Button">
<span class="ms-Button-label">Run</span>
</button>
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/beta/hosted/office.js
@types/office-js-preview
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
core-js@2.4.1/client/core.min.js
@types/core-js
jquery@3.1.1
@types/jquery@3.3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment