- Clone the repo
git clone [email protected]:HeroLink/excel-add-in.git
. - Run
npm install
. - For development, run a
webpack serve
, usenpm run dev-server
.- It may take one minute to start the dev-server.
- For build, run
npm run build
. - After running
webpack serve
, typenpm run start:desktop
to start an Excel.- The add-in is loaded by a task pane.
- Click
Show Taskpane
on the Ribbon, a pane will be opened. - Click
Run
to dynamically register custom functions. - Type the following in cells:
=XLP.GETWEATHER("Beijing", "China")
: Get the weather of Beijing in China. The API is provided by Rapid API.=XLP.GETSTARCOUNT("officedev","office-js")
: Get the start count of the office-js repo.=XLP.CLOCK()
: Display the current time once a second.=XLP.ADD(A1, A2)
: Add two numbers.=XLP.LOG("Message")
: Write a message to console.log().=XLP.INCREMENT(value)
: Increment a value once a second.
Most code is in src/utils/*
. The code is from the script-lab repo.
Key code in src\taskpane\taskpane.ts
:
await fetch("https://localhost:3000/tests/custom-functions/functions.ts")
.then((res) => {
console.log("Fetch function.ts", res);
return res.text();
})
.then((data) => {
// console.log("Read functions.ts", data);
dynamicRegisterCF(data);
});
dynamicRegisterCF()
in src\utils\custom-functions\register.ts
:
export async function dynamicRegisterCF(fileContent: string) {
// const engineStatus = await getCustomFunctionEngineStatusSafe();
// parse custom functions file
const { parseResults, code } = await getRegistrationResult(fileContent);
console.log("Parsed results", parseResults);
// console.log("Codes in file", code);
if (parseResults.length > 0) {
// do registration
await registerCustomFunctions(parseResults, code);
console.log("Register custom functions successfully!");
}
// add iframe runner
tryCatch(async () => {
const CustomFunctionsDictionary = {};
(window as any).CustomFunctionsDictionary = CustomFunctionsDictionary;
const typescriptMetadata = await getMetadata(fileContent);
console.log("Get typescriptMetadata", typescriptMetadata);
await addIframe(typescriptMetadata);
console.log("CustomFunctionsDictionary", CustomFunctionsDictionary);
// associate functions' id and name
for (const key in CustomFunctionsDictionary) {
CustomFunctions.associate(key, CustomFunctionsDictionary[key]);
console.log("key", key);
console.log("CustomFunctionsDictionary", CustomFunctionsDictionary[key]);
}
});
}