Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
wandyezj committed Jun 5, 2024
1 parent 163b4f8 commit a68e75d
Showing 1 changed file with 92 additions and 1 deletion.
93 changes: 92 additions & 1 deletion src/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ActionType, TriggerAction, TriggerType } from "./core/actions/TriggerAction";
import {
Action,
ActionType,
TriggerAction,
TriggerExcelNamedRangeEdit,
TriggerType,
} from "./core/actions/TriggerAction";

// cspell:ignore SHOWTASKPANE HIDETASKPANE addin
console.log("actions load");
Expand Down Expand Up @@ -61,8 +67,88 @@ const triggerActionExcelNamedRangeTest: TriggerAction = {
},
};

/**
* action registry
*/
const triggerActions: TriggerAction[] = [triggerActionLoadLog, triggerActionExcelNamedRangeTest];

function runActionLogId(triggerAction: TriggerAction) {
console.log(triggerAction.id);
}

/**
* Actions types to runs.
*/
const actionMap = new Map<string, (triggerAction: TriggerAction) => void>([[ActionType.LogId, runActionLogId]]);

function runAction(triggerAction: TriggerAction) {
const actionType = triggerAction.action.type;
const handler = actionMap.get(actionType);

if (handler === undefined) {
console.log(`Handler for actionType ${actionType}`);
} else {
handler(triggerAction);
}
}

function getTriggerTypes(triggerActions: TriggerAction[], triggerType: TriggerType) {
const triggers = triggerActions.filter((triggerAction) => triggerAction.trigger.type === triggerType);
return triggers;
}

function runTriggersActions(triggers: TriggerAction[]) {
triggers.forEach((trigger) => {
runAction(trigger);
});
}

/**
* Trigger all load triggers
*/
function triggerLoad() {
const triggers = getTriggerTypes(triggerActions, TriggerType.Load);
runTriggersActions(triggers);
}

/**
* Trigger all Excel Named Range Edit triggers relevant to the event.
* @param event
*/
async function triggerExcelNamedRangeEdit(event: { worksheetId: string; rangeAddress: string }) {
const triggers = getTriggerTypes(triggerActions, TriggerType.ExcelNamedRangeEdit) as {
id: string;
trigger: TriggerExcelNamedRangeEdit;
action: Action;
}[];

// Check if the Range edit action matches a trigger
const matching = await Excel.run(async (context) => {
const workbook = context.workbook;

const intersects = triggers.map((trigger, index) => {
const namedItemName = trigger.trigger.parameters.namedRangeName;
const namedItem = workbook.names.getItemOrNullObject(namedItemName);
const range = namedItem.getRangeOrNullObject();

const eventRange = workbook.worksheets.getItemOrNullObject(event.worksheetId).getRange(event.rangeAddress);
const intersection = range.getIntersectionOrNullObject(eventRange);
return { intersection, index };
});

await context.sync();

const match = intersects
.filter(({ intersection }) => !intersection.isNullObject)
.map(({ index }) => triggers[index]);
return match;
});

runTriggersActions(matching);
}

//async function registerTriggerExcelNamedRangeEdit() {}

async function registerTriggerActions() {
console.log("Register Triggers");

Expand All @@ -88,6 +174,8 @@ async function registerTriggerActions() {
const eventWorksheetId = event.worksheetId;
const eventAddress = event.address;

triggerExcelNamedRangeEdit({ worksheetId: eventWorksheetId, rangeAddress: eventAddress });

console.log(eventWorksheetId);
console.log(eventAddress);
// details only useful for single cell edits
Expand Down Expand Up @@ -119,5 +207,8 @@ Office.onReady(() => {
console.log("ready");
// Requires trigger of the runtime before it will automatically start up.
Office.addin.setStartupBehavior(Office.StartupBehavior.load);

triggerLoad();

registerTriggerActions();
});

0 comments on commit a68e75d

Please sign in to comment.