Skip to content

Instantly share code, notes, and snippets.

@sancarn
Created December 8, 2018 16:36
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 sancarn/0bf92b1ec5e4c6d28794714c31d5da02 to your computer and use it in GitHub Desktop.
Save sancarn/0bf92b1ec5e4c6d28794714c31d5da02 to your computer and use it in GitHub Desktop.
Shared with Script Lab
name: VbaJsBridge
description: ''
author: sancarn
host: EXCEL
api_set: {}
script:
content: |-
//Initialise VbaJsBridge
VbaJsBridge_Initiate();
var Timer: Number = 1000;
function VbaJsBridge_EvalVBA() {
}
function VbaJsBridge_HandleXML(xml: XMLDocument) {
//xml : XMLDocument {...}
//Custom data handling at the moment...
var execs = xml.getElementsByTagName("js-exec")
for (var i = 0; i < execs.length; i++) {
//Get executable element
var element = execs[i];
//element: Element {clientHeight = 0, clientLeft = 0, clientTop = 0, ...}
//Evaluate node's content
var result = undefined;
try {
result = eval(element.textContent);
} catch (e) {
console.error(e);
}
//Return result if id is supplied and result is given.
if (result != undefined) {
var id: string = element.getAttribute("id");
if (id) {
var resultNode = document.createElement("js-result")
try {
var sResult: string = JSON.stringify(result);
} catch (e) {
var x: Error = e;
var sResult: string = "CANNOT SERIALISE: " + x.name + "-" + x.message
}
resultNode.innerText = sResult;
resultNode.setAttribute("id", id);
element.parentNode.appendChild(resultNode);
}
}
//Remove node from parent
element.parentNode.removeChild(element);
}
return xml;
}
async function VbaJsBridge_Initiate() {
//Monitor for new changes in data
VbaJsBridge_MonitorChanges();
}
async function VbaJsBridge_MonitorChanges() {
//Every 100ms check for changes in data
setTimeout(async function () {
//set data
await getVbaJSBridge_MonitorHandler();
//Continue monitoring...
VbaJsBridge_MonitorChanges();
}, Timer);
}
async function getVbaJSBridge_MonitorHandler() {
return await Excel.run(async (context) => {
// You must have the xmlns attribute to populate the
// CustomXml.namespaceUri property.
var elements = context.workbook.customXmlParts.getByNamespace("VBA-JS-Bridge");
elements.load("values");
await context.sync();
//Get XML
const customXmlPart: Excel.CustomXmlPart = elements.items[0];
const xmlBlob = customXmlPart.getXml();
await context.sync();
//Extract XML data
console.log(xmlBlob.value);
var xml = jQuery.parseXML(xmlBlob.value);
//Handle xml
xml = VbaJsBridge_HandleXML(xml)
//Set newXML data
var sXML = (new XMLSerializer()).serializeToString(xml);
customXmlPart.setXml(sXML);
console.log(sXML);
await context.sync();
return
});
}
/** 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: |
<p>Hello world</p>
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
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
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.min.js
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.d.ts
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