Skip to content

Instantly share code, notes, and snippets.

View davecra's full-sized avatar
💭
Coding for arrays

David E. Craig davecra

💭
Coding for arrays
View GitHub Profile
@davecra
davecra / inline3.js
Last active May 12, 2023 22:04
inline3.js
export default class MyClassToCallTheServer {
#internvalValue = null;
#loading = false;
constrcutor() {
this.#internalValue = this.#callTheServer();
}
doSomethingWithTheServerData = async () => {
while(this.#loading) await new Promise(resolve => setTimeout(resolve, 10));
// the #internalValue is not null because we waited
return this.#internalValue;
@davecra
davecra / inline2.js
Last active May 12, 2023 22:05
inline2.js
export default class MyClassToCallTheServer {
#internvalValue = null;
constrcutor() {
// this.#internalValue = await this.#callTheServer(); <-- the await will fail to compile
this.#internalValue = this.#callTheServer();
}
doSomethingWithTheServerData = () => {
// the #internalValue is still null becasue we could not await in the constructor
return this.#internalValue;
}
@davecra
davecra / inline1.js
Last active May 12, 2023 21:55
inline1.js
const myObj = new MyClassToCallTheServer();
let someValue = myObj.doSomegthingWithTheServerData();
@davecra
davecra / detectMimeType.js
Created December 31, 2022 02:53
A JavaScript function to detect the Media/Mime Type for a base64 string
/**
* Returns the data type based on the base64 string
* @param {String} base64String
* @param {String} fileName
* @returns {String}
*/
detectMimeType(base64String, fileName) {
var ext = fileName.substring(fileName.lastIndexOf(".") + 1);
if (ext === undefined || ext === null || ext === "") ext = "bin";
ext = ext.toLowerCase();
@davecra
davecra / getUsername.js
Created July 27, 2020 14:51
Gets the username for the current user using the SSO API
export async function getUserName() {
try {
let tokenData = await OfficeRuntime.auth.getAccessToken({ allowSignInPrompt: false, forMSGraphAccess: true });
var parts = tokenData.split(".");
var token = JSON.parse(atob(parts[1]));
return token.preferred_username;
}
catch (exception) {
console.log(exception.message);
}
@davecra
davecra / SendCustomXmlPartMessage.js
Created June 15, 2020 19:55
This is the code from a Task Pane Add-in that sends a message to a Content Add-in via a CustomXMLPart
const ns = "http://pfe.microsoft.com/excelpoc/1.0";
const xml = "<message xmlns='http://pfe.microsoft.com/excelpoc/1.0'>" +
"<sentby>[who]</sentby>" +
"<info>[data]</info>" +
"</message>";
const from_tp = "TASKPANE ADD-IN";
function sendMessage() {
Excel.run(function(context) {
var data = xml.replace("[who]", from_tp).replace("[data]", "This message is coming from the taskpane.");
const customXmlPart = context.workbook.customXmlParts.add(data);
@davecra
davecra / ContentAddInReadCustomXmlPart.js
Last active June 15, 2020 20:07
This is a function from a Content Add-in looks for a specific customXMLPart in the document as a message from a TaskPane Add-in
Office.initialize = function(reason) {
// background thread checker
window.setInterval(() => { checkForPart(); }, 1000);
}
const ns = "http://pfe.microsoft.com/excelpoc/1.0";
const xml = "<message xmlns='http://pfe.microsoft.com/excelpoc/1.0'>" +
"<sentby>[who]</sentby>" +
"<info>[data]</info>" +
"</message>";
@davecra
davecra / DetectUserPrintInOutlook.cs
Created June 5, 2020 17:35
Using a background thread and Windows API's in Outlook, we can detect when the user has opened the Print tab in the backstage
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
/// <summary>
/// Startup for Outlook Add-in
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ThisAddIn_Startup(object sender, System.EventArgs e)
@davecra
davecra / plugins.js
Created May 29, 2020 20:12
Plugins for all OfficeJS Parts
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
filename: "taskpane.html",
template: "./src/dialogs/taskpane.html",
chunks: ["polyfill", "common", "shared", "taskpane"]
}),
new HtmlWebpackPlugin({
filename: "dialog.html",
template: "./src/dialogs/dialog.html",
@davecra
davecra / entry.js
Created May 29, 2020 20:08
Web Pack Entry
entry: {
polyfill: "@babel/polyfill",
dialog: "./src/dialogs/dialog.js",
commands: "./src/commands/commands.js",
shared: "./src/common/shared.js",
common: "./src/common/common.js",
taskpane: "./src/taskpane/taspane.js"
},