I’m trying to get my first proper Flow in Power Automate working.
Purpose of this flow is to check for the latest montly report, add up all the products within one name and print them on another sheet. I have an Office Script that successfully performs the changes when executed with a button directly within Excel Online. However, when I integrate this script into a Power Automate flow, the flow runs without errors, but no changes are made to the Excel file itself.
function main(workbook: ExcelScript.Workbook) {
let wsOversicht = workbook.getWorksheet("Übersicht");
let today = new Date();
let currentMonth = today.getMonth() + 1; // Current month (1-based)
let monthName = today.toLocaleString('default', { month: 'long' });
let wsData: ExcelScript.Worksheet | undefined = workbook.getWorksheet(monthName);
if (wsData) {
// Clear Worksheet "Übersicht"
let lastRowOversicht = wsOversicht.getUsedRange()?.getLastRow()?.getRowIndex() ?? 1;
if (lastRowOversicht > 1) {
wsOversicht.getRange(`A2:B${lastRowOversicht + 1}`).clear();
}
// Create Headers for Column A & B in cell A1 & B1
wsOversicht.getRange("A1").setValue("Produkt");
wsOversicht.getRange("B1").setValue(`Menge ${monthName}`);
// Find last row of current Months sheet
let lastRowData = wsData.getUsedRange()?.getLastRow()?.getRowIndex() ?? 1;
// Read all names and add up corresponding numbers
let articleRange = wsData.getRange(`E2:E${lastRowData + 1}`).getValues();
let mengeRange = wsData.getRange(`M2:M${lastRowData + 1}`).getValues();
let overviewData: { [key: string]: number } = {};
// Loop for all products in sheet
for (let i = 0; i < articleRange.length; i++) {
let article = articleRange[i][0];
let menge = mengeRange[i][0];
// Initialize writing of product, if not in list yet
if (!overviewData.hasOwnProperty(article)) {
overviewData[article] = 0;
}
if (typeof menge === "number") {
overviewData[article] += menge;
}
}
// Writing data into Overview
let overviewEntries = Object.entries(overviewData);
let outputRange = wsOversicht.getRange(`A2:B${overviewEntries.length + 1}`);
let outputValues = overviewEntries.map(([article, menge]) => [article, menge]);
outputRange.setValues(outputValues);
} else {
console.log(`Das Monatsdatenblatt '${monthName}' wurde nicht gefunden.`);
}
}
I’ve looked up solutions online (e.g. acess to the file, removal of ActiveWorksheet, adding in 10 seconds of delay before and after running the script), but haven’t found one thats solving my problem.
Can anyone suggest what might be going wrong or how I can troubleshoot this issue effectively? Even a tendency of whether it’s a coding or flow issue is gonna help heaps.