Created
September 20, 2018 02:20
-
-
Save salcosta/3cbed492896c6bb6b65f7479cf28fa95 to your computer and use it in GitHub Desktop.
Shared with Script Lab
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: EEX Example | |
description: '' | |
author: salcosta | |
host: EXCEL | |
api_set: {} | |
script: | |
content: | | |
$("#run").click(() => tryCatch(run)); | |
async function run() { | |
await Excel.run(async (context) => { | |
var clientIP = await getIP(); | |
var url = "https://www.eex-transparency.com/static/tem-10a-aggregated/de/2018/09/10.json"; | |
var params = generateCryptedParams(url, clientIP); | |
console.log(params); | |
try { | |
var fullURL = escapeHashSign(url + '?expires=' + params.expires + '&md5=' + params.md5); | |
console.log(fullURL) | |
var result = await jQuery.ajax({ | |
url: fullURL, | |
method: 'GET', | |
dataType: 'json', | |
headers: { | |
'Accept': 'application/json, text/plain, */*', | |
'Accept-Encoding': 'gzip, deflate, br', | |
'Accept-Language': 'en-US,en;q=0.9', | |
'Referer': 'https://www.eex-transparency.com/homepage/power/germany/production/usage/actual-production-100-mw-/actual-production-100-mw-table' | |
} | |
}); | |
console.log(result); | |
} catch (ex) { | |
console.log(ex); | |
} | |
//OfficeHelpers.UI.notify(JSON.stringify(params)); | |
await context.sync(); | |
}); | |
} | |
/** 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); | |
} | |
} | |
async function getIP() { | |
var result = await jQuery.get('https://api.ipify.org?format=json'); | |
return result.ip; | |
} | |
function generateCryptedParams(url, clientIP) { | |
var md5, md5Encoded, md5WithoutSpeciaChars, parser, timePoint, urlPath; | |
var timePoint = moment().tz("Europe/Berlin").add(1, "minute").unix(); | |
parser = document.createElement("a"); | |
parser.href = url; | |
var urlPath = parser.pathname; | |
"/" !== urlPath[0] && (urlPath = "/" + urlPath); | |
md5 = CryptoJS.MD5(urlPath + timePoint + clientIP + " zYeHzBomGdgV"); | |
md5Encoded = md5.toString(CryptoJS.enc.Base64); | |
md5WithoutSpeciaChars = replaceSpecialChars(md5Encoded); | |
return { | |
"expires": timePoint, | |
"md5": md5WithoutSpeciaChars | |
} | |
} | |
function replaceSpecialChars(str) { | |
var key, specialChars, value; | |
specialChars = { | |
"=": "", | |
"\\+": "-", | |
"/": "_", | |
"%": "_" | |
}; | |
for (key in specialChars) | |
value = specialChars[key], | |
str = str.replace(new RegExp(key, "g"), value); | |
return str | |
} | |
function escapeHashSign(url) { | |
return url.replace(/#/g, "%23"); | |
} | |
language: typescript | |
template: | |
content: | | |
<button id="run" class="ms-Button"> | |
<span class="ms-Button-label">Run</span> | |
</button> | |
language: html | |
style: | |
content: | | |
/* Your style goes here */ | |
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 | |
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js | |
@types/momentjs | |
https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js | |
@types/cryptojs | |
https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data.min.js | |
@types/momentjstz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment