web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Bad Script Error when ...
Power Automate
Unanswered

Bad Script Error when I run my Office Script with Power Automate

(1) ShareShare
ReportReport
Posted on by 4
 

Here is my office script which I run from Power Automate, I get error saying Bad Gateway but when I run the same script in excel it runs fine

 

function main(workbook: ExcelScript.Workbook) {
  const sheet = workbook.getActiveWorksheet();
  const startRow = 10; // Row 11 in Excel (0-based index)
  const usedRange = sheet.getUsedRange();
  const values = usedRange.getValues();
  // Get the header row at row 11
  const headers = values[startRow] as string[];
  const dateColIndex = headers.indexOf("Date");
  const durationColIndex = headers.indexOf("Duration (Hours)");
  const commentColIndex = headers.indexOf("Comment");
  const projectColIndex = headers.indexOf("Project");
  const activityColIndex = headers.indexOf("Activity");
  const nameColIndex = headers.indexOf("Name");
  const employeeIdColIndex = headers.indexOf("Employee ID");
  if (dateColIndex === -1) throw new Error("No 'Date' column found in header row.");
  if (durationColIndex === -1) throw new Error("No 'Duration (Hours)' column found in header row.");
  if (projectColIndex === -1) throw new Error("No 'Project' column found in header row.");
  // Extract data rows (rows after the header)
  const dataRows = values.slice(startRow + 1);
  // Process data rows
  dataRows.forEach(row => {
    const duration = parseFloat(row[durationColIndex] as string);
    const project = row[projectColIndex]?.toString().trim();
    const activity = row[activityColIndex]?.toString().trim();
    if (!isNaN(duration) && duration > 9) {
      row[durationColIndex] = 9;
    }
    if (!isNaN(duration as number) && (duration as number) > 7) {
      row[durationColIndex] = 9;
    }
    // Remove duration for Leave, Weekend, or Public Holiday
    if (["Leave", "Weekend", "Public Holiday"].includes(project)) {
      row[durationColIndex] = "";
    }
    if (activity && ["Week-Off", "Week Off"].includes(activity)) {
      row[durationColIndex] = "";
      row[commentColIndex] = "";
    }
    if (["Weekend", "Public Holiday"].includes(project)) {
      row[commentColIndex] = "";
    }
    if (["Leave"].includes(project)) {
      if (!["Half day"].includes(activity)) {
        row[commentColIndex] = "";
      }
    }
  });
  // Parse all dates and determine month/year
  const allDates: Date[] = [];
  dataRows.forEach(row => {
    const dateStr = row[dateColIndex]?.toString();
    if (dateStr) {
      const date = parseDateString(dateStr);
      if (!isNaN(date.getTime())) {
        allDates.push(date);
      }
    }
  });
  if (allDates.length === 0) throw new Error("No valid dates found in data.");
  // Get the month and year from the first date
  const firstDate = allDates[0];
  const month = firstDate.getMonth();
  const year = firstDate.getFullYear();
  // Determine days in month
  const daysInMonth = new Date(year, month + 1, 0).getDate();
  // Create a Set of existing dates in DD-MM-YYYY format
  const existingDates = new Set<string>();
  allDates.forEach(date => {
    const dateStr = formatDate(date);
    existingDates.add(dateStr);
  });
  // Pre-calculate all weekends in the month
  const weekends: { dateStr: string, dayName: string }[] = [];
  for (let day = 1; day <= daysInMonth; day++) {
    const date = new Date(year, month, day);
    const dayOfWeek = date.getDay();
    if (dayOfWeek === 0 || dayOfWeek === 6) { // Sunday or Saturday
      weekends.push({
        dateStr: formatDate(date),
        dayName: dayOfWeek === 0 ? "Sunday" : "Saturday"
      });
    }
  }
  // Find missing dates
  const missingDates: { dateStr: string, isWeekend: boolean, dayName?: string }[] = [];
  for (let day = 1; day <= daysInMonth; day++) {
    const date = new Date(year, month, day);
    const dateStr = formatDate(date);
    if (!existingDates.has(dateStr)) {
      const dayOfWeek = date.getDay();
      const isWeekend = dayOfWeek === 0 || dayOfWeek === 6;
      missingDates.push({
        dateStr: dateStr,
        isWeekend: isWeekend,
        dayName: isWeekend ? (dayOfWeek === 0 ? "Sunday" : "Saturday") : undefined
      });
    }
  }
  // Get sample data for employee info (from first row)
  const sampleRow = dataRows[0];
  const employeeId = sampleRow[employeeIdColIndex];
  const name = sampleRow[nameColIndex];
  // Create rows for missing dates
  const missingRows = missingDates.map(missing => {
    const newRow = [...sampleRow] as (string | number)[];
    newRow[dateColIndex] = missing.dateStr;
    if (missing.isWeekend) {
      newRow[projectColIndex] = "Weekend";
      if (activityColIndex !== -1) newRow[activityColIndex] = missing.dayName;
      newRow[durationColIndex] = "";
    } else {
      newRow[projectColIndex] = "";
      if (activityColIndex !== -1) newRow[activityColIndex] = "";
      newRow[durationColIndex] = "";
    }
    return newRow;
  });
  // Combine existing and missing rows
  const allDataRows = [...dataRows, ...missingRows];
  // Sort by Date (proper date comparison)
  allDataRows.sort((a, b) => {
    const dateA = parseDateString(a[dateColIndex]?.toString());
    const dateB = parseDateString(b[dateColIndex]?.toString());
    return dateA.getTime() - dateB.getTime();
  });
  // Sort by Name (alphabetical)
  allDataRows.sort((a, b) => {
    const nameA = (a[nameColIndex] || '').toString().toLowerCase();
    const nameB = (b[nameColIndex] || '').toString().toLowerCase();
    return nameA.localeCompare(nameB);
  });
  // Recombine: everything above row 11 + header + sorted, processed data
  const finalValues = [
    ...values.slice(0, startRow), // rows above header
    headers,
    ...allDataRows
  ];
  // Resize and write back to sheet
  const newRange = sheet.getRangeByIndexes(0, 0, finalValues.length, finalValues[0].length);
  newRange.setValues(finalValues);
  // Center align the Duration (Hours) column
  const durationColumnRange = newRange.getColumn(durationColIndex);
  durationColumnRange.getFormat().setHorizontalAlignment(ExcelScript.HorizontalAlignment.center);
  // Apply font styling to all cells
  const entireRange = sheet.getUsedRange();
  entireRange.getFormat().getFont().setName("Aptos");
  entireRange.getFormat().getFont().setSize(10);
  // Check if all entries in "Comment" column are empty
  const allCommentsEmpty = allDataRows.every(row => {
    const val = row[commentColIndex];
    return val === null || val === undefined || val.toString().trim() === "";
  });
  if (allCommentsEmpty) {
    // Try to find the table object if present
    const tables = sheet.getTables();
    const targetTable = tables.find(table => {
      const headerNames = table.getHeaderRowRange().getValues()[0];
      return headerNames.includes("Comment");
    });
    if (targetTable) {
      // Convert table to range before deleting column
      targetTable.convertToRange();
    }
    // Delete the Comment column from the entire worksheet
    sheet.getRangeByIndexes(0, commentColIndex, sheet.getUsedRange().getRowCount(), 1).delete(ExcelScript.DeleteShiftDirection.left);
    // Fix right border for the new last column after deleting "Comment"
    const updatedUsedRange = sheet.getUsedRange();
    const newLastColIndex = updatedUsedRange.getColumnCount() - 1;
    const rowCount = updatedUsedRange.getRowCount();
    // Apply right border to each cell in the new last column
    for (let i = 0; i < rowCount; i++) {
      const cell: ExcelScript.Range = sheet.getCell(i, newLastColIndex);
      const borders: ExcelScript.RangeBorder[] = cell.getFormat().getBorders();
      for (let j = 0; j < borders.length; j++) {
        const border: ExcelScript.RangeBorder = borders[j];
        if (border.getSideIndex() === ExcelScript.BorderIndex.edgeRight) {
          border.setStyle(ExcelScript.BorderLineStyle.continuous);
          border.setWeight(ExcelScript.BorderWeight.thick);
          border.setColor("black");
        }
      }
    }
  }
  // === STEP 1: Convert the table to a range (required before merging cells) ===
  const allTables = sheet.getTables();
  if (allTables.length > 0) {
    allTables[0].convertToRange();
  }
  // === STEP 2: Merge Activity and Duration columns for 'Weekend' rows ===
  const updatedRange = sheet.getUsedRange();
  const updatedValues = updatedRange.getValues();
  // Find new indexes after possible Comment column deletion
  const updatedHeaders = updatedValues[startRow] as string[];
  const updatedProjectColIndex = updatedHeaders.indexOf("Project");
  const updatedActivityColIndex = updatedHeaders.indexOf("Activity");
  const updatedDurationColIndex = updatedHeaders.indexOf("Duration (Hours)");
  // Loop through rows below the header and apply merging
  for (let i = startRow + 1; i < updatedValues.length; i++) {
    const row = updatedValues[i];
    const project = row[updatedProjectColIndex]?.toString().trim();
    if (project === "Weekend") {
      const rowIndex = i;
      const rangeToMerge = sheet.getRangeByIndexes(rowIndex, updatedActivityColIndex, 1, 2);
      rangeToMerge.merge(true); // Merge and center
      rangeToMerge.getFormat().setHorizontalAlignment(ExcelScript.HorizontalAlignment.center); // Center align
      const value = row[updatedActivityColIndex];
      sheet.getCell(rowIndex, updatedActivityColIndex).setValue(value);
    }
  }
  // === STEP 3: Merge Project + Activity + Duration columns for 'Leave' and 'Public Holiday' rows ===
for (let i = startRow + 1; i < updatedValues.length; i++) {
  const row = updatedValues[i];
  const project = row[updatedProjectColIndex]?.toString().trim();
  if (project === "Leave" || project === "Public Holiday") {
    const rowIndex = i;
    const rangeToMerge = sheet.getRangeByIndexes(rowIndex, updatedProjectColIndex, 1, 3);
    rangeToMerge.merge(true); // Merge and center
    rangeToMerge.getFormat().setHorizontalAlignment(ExcelScript.HorizontalAlignment.center); // Center align
    const value = row[updatedActivityColIndex];
    sheet.getCell(rowIndex, updatedProjectColIndex).setValue(value);
  }
}
}
// Helper function to parse DD-MM-YYYY date strings
function parseDateString(dateStr: string | undefined): Date {
  if (!dateStr) return new Date(NaN);
  const parts = dateStr.split('-');
  if (parts.length !== 3) return new Date(NaN);
  const day = parseInt(parts[0], 10);
  const month = parseInt(parts[1], 10) - 1; // JavaScript months are 0-based
  const year = parseInt(parts[2], 10);
  return new Date(year, month, day);
}
// Helper function to format date as DD-MM-YYYY
function formatDate(date: Date): string {
  return `${date.getDate().toString().padStart(2, '0')}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getFullYear()}`;
}

 
Categories:
I have the same question (0)
  • MarkRahn Profile Picture
    1,231 Super User 2025 Season 2 on at
    Hi,
     
    With the limited information you provided, I tried to take a look at what you might be seeing.
     
    I created a simple Excel file with the following:
     
    I put the files in a SharePoint Document Library. I took your script and saved the osts file to the same SharePoint document Library.
     
    I modified the script and set the startRow to 0:
     
    I created a simple flow using the "Run script from SharePoint library" Action:
     
    The flow ran and I get this error:
     
    So I have some questions for you which will hopefully get us to why you are seeing "Bad Gateway" which typically points to a Connection issue.
     
    Questions:
    1) What Action are you using in your Flow to run the Script? I used the one that runs a Shared Script stored in SharePoint to do my test. I also tested with the "Run Script" Action which was also able to run a Script stored in the Excel file.
     
    2) Where is your file located and what permissions are set on the file?
     
    3) Are you able to create a simple Script in an Excel file and run that from a flow? 
     
    4) Do you currently have the Excel file open on your desktop? It may be locked which could be causing the error you are seeing.
     
    5) What account is being used for the Connection to Excel? On the Action, click on the "...". What does it say under "My Connections"?
     
    6) When you run this script in Excel manually, how long does it take to run? Is it greater than 5 minutes? If so, the connection may be timing out.
     
    This community is supported by individuals freely devoting their time to answer questions and provide support. They do it to let you know you are not alone. This is a community.

    If someone has been able to answer your questions or solve your problem, please click Does this answer your question. This will help others who have the same question find a solution quickly via the forum search.

    If someone was able to provide you with more information that moved you closer to a solution, throw them a Like. It might make their day. 😊

    Thanks
    -Mark
     
  • PS-06060944-0 Profile Picture
    4 on at
    @MarkRahn Thanks for the reply, I have modified the script a bit so here is my updated script, please note now the script runs fine every single time I run it directly from excel but when I run power automate I am generating multiple excel files which has almost 50 lines of entries, if I am generating 2 to 3 excel files then again the script works but if I am generating more than 7 files I get Bad Request Error
     
    Here are the answers to your question
     
    1. I use Run Script action
    2. The files are generated from Power automate workflow itself and are stored in Sharepoint documents
    3. Yes I am able to create a simple Script in an Excel file and run that from a flow
    4. No I do not have the file open as the file is generated by Power Automate
    5. pritish@transformhub.com
    6. Its pretty quick with my updated code, maybe 10 seconds max
     
    Here is my updated script
     
    function main(workbook: ExcelScript.Workbook) {
    const sheet = workbook.getActiveWorksheet();
    const startRow = 10; // Row 11 in Excel (0-based index)
    const usedRange = sheet.getUsedRange();
    const values = usedRange.getValues();
     

     

    // Get the header row at row 11
    const headers = values[startRow] as string[];
    const dateColIndex = headers.indexOf("Date");
    const durationColIndex = headers.indexOf("Duration (Hours)");
    const commentColIndex = headers.indexOf("Comment");
    const projectColIndex = headers.indexOf("Project");
    const activityColIndex = headers.indexOf("Activity");
    const nameColIndex = headers.indexOf("Name");
    const employeeIdColIndex = headers.indexOf("Employee ID");
     

     

    if (dateColIndex === -1) throw new Error("No 'Date' column found in header row.");
    if (durationColIndex === -1) throw new Error("No 'Duration (Hours)' column found in header row.");
    if (projectColIndex === -1) throw new Error("No 'Project' column found in header row.");
     

     

    // Extract data rows (rows after the header)
    const dataRows = values.slice(startRow + 1);
     

     

    // Process data rows
    dataRows.forEach(row => {
    const duration = parseFloat(row[durationColIndex] as string);
    const project = row[projectColIndex]?.toString().trim();
    const activity = row[activityColIndex]?.toString().trim();
     

     

    if (!isNaN(duration) && duration > 9) {
    row[durationColIndex] = 9;
    }
     

     

    if (!isNaN(duration as number) && (duration as number) > 7) {
    row[durationColIndex] = 9;
    }
     

     

    // Remove duration for Leave, Weekend, or Public Holiday
    if (["Leave", "Weekend", "Public Holiday"].includes(project)) {
    row[durationColIndex] = "";
    }
     

     

    if (activity && ["Week-Off", "Week Off"].includes(activity)) {
    row[durationColIndex] = "";
    row[commentColIndex] = "";
    }
     

     

    if (["Weekend", "Public Holiday"].includes(project)) {
    row[commentColIndex] = "";
    }
     

     

    if (["Leave"].includes(project)) {
    if (!["Half day"].includes(activity)) {
    row[commentColIndex] = "";
    }
     

     

    }
    });
     

     

    // Parse all dates and determine month/year
    const allDates: Date[] = [];
    dataRows.forEach(row => {
    const dateStr = row[dateColIndex]?.toString();
    if (dateStr) {
    const date = parseDateString(dateStr);
    if (!isNaN(date.getTime())) {
    allDates.push(date);
    }
    }
    });
     

     

    if (allDates.length === 0) throw new Error("No valid dates found in data.");
     

     

    // Get the month and year from the first date
    const firstDate = allDates[0];
    const month = firstDate.getMonth();
    const year = firstDate.getFullYear();
     

     

    // Determine days in month
    const daysInMonth = new Date(year, month + 1, 0).getDate();
     

     

    // Create a Set of existing dates in DD-MM-YYYY format
    const existingDates = new Set<string>();
    allDates.forEach(date => {
    const dateStr = formatDate(date);
    existingDates.add(dateStr);
    });
     

     

    // Pre-calculate all weekends in the month
    const weekends: { dateStr: string, dayName: string }[] = [];
    for (let day = 1; day <= daysInMonth; day++) {
    const date = new Date(year, month, day);
    const dayOfWeek = date.getDay();
    if (dayOfWeek === 0 || dayOfWeek === 6) { // Sunday or Saturday
    weekends.push({
    dateStr: formatDate(date),
    dayName: dayOfWeek === 0 ? "Sunday" : "Saturday"
    });
    }
    }
     

     

    // Find missing dates
    const missingDates: { dateStr: string, isWeekend: boolean, dayName?: string }[] = [];
    for (let day = 1; day <= daysInMonth; day++) {
    const date = new Date(year, month, day);
    const dateStr = formatDate(date);
     

     

    if (!existingDates.has(dateStr)) {
    const dayOfWeek = date.getDay();
    const isWeekend = dayOfWeek === 0 || dayOfWeek === 6;
    missingDates.push({
    dateStr: dateStr,
    isWeekend: isWeekend,
    dayName: isWeekend ? (dayOfWeek === 0 ? "Sunday" : "Saturday") : undefined
    });
    }
    }


     

    // Get sample data for employee info (from first row)
    const sampleRow = dataRows[0];
    const employeeId = sampleRow[employeeIdColIndex];
    const name = sampleRow[nameColIndex];
     

     

    // Create rows for missing dates
    const missingRows = missingDates.map(missing => {
    const newRow = [...sampleRow] as (string | number)[];
    newRow[dateColIndex] = missing.dateStr;
     

     

    if (missing.isWeekend) {
    newRow[projectColIndex] = "Weekend";
    if (activityColIndex !== -1) newRow[activityColIndex] = missing.dayName;
    newRow[durationColIndex] = "";
    } else {
    newRow[projectColIndex] = "";
    if (activityColIndex !== -1) newRow[activityColIndex] = "";
    newRow[durationColIndex] = "";
    }
     

     

    return newRow;
    });
     

     

    // Combine existing and missing rows
    const allDataRows = [...dataRows, ...missingRows];
     

     

    // Sort by Date (proper date comparison)
    allDataRows.sort((a, b) => {
    const dateA = parseDateString(a[dateColIndex]?.toString());
    const dateB = parseDateString(b[dateColIndex]?.toString());
    return dateA.getTime() - dateB.getTime();
    });
     

     

    // Sort by Name (alphabetical)
    allDataRows.sort((a, b) => {
    const nameA = (a[nameColIndex] || '').toString().toLowerCase();
    const nameB = (b[nameColIndex] || '').toString().toLowerCase();
    return nameA.localeCompare(nameB);
    });
     

     

    allDataRows.forEach(row => {
    const project = row[projectColIndex]?.toString().trim();
    const activity = row[activityColIndex]?.toString().trim();
     

     

    if (activity && ["Week-Off", "Week Off"].includes(activity)) {
    row[durationColIndex] = "";
    row[commentColIndex] = "";
    }
     

     

    if (["Weekend", "Public Holiday"].includes(project)) {
    row[commentColIndex] = "";
    }
     

     

    if (["Leave"].includes(project)) {
    if (!["Half day"].includes(activity)) {
    row[commentColIndex] = "";
    }
     

     

    }
    });
     

     

    // Recombine: everything above row 11 + header + sorted, processed data
    const finalValues = [
    ...values.slice(0, startRow), // rows above header
    headers,
    ...allDataRows
    ];
     

     

    // Resize and write back to sheet
    const newRange = sheet.getRangeByIndexes(0, 0, finalValues.length, finalValues[0].length);
    newRange.setValues(finalValues);
     

     

    // Center align the Duration (Hours) column
    const durationColumnRange = newRange.getColumn(durationColIndex);
    durationColumnRange.getFormat().setHorizontalAlignment(ExcelScript.HorizontalAlignment.center);
     

     

    // Apply font styling to all cells
    const entireRange = sheet.getUsedRange();
    entireRange.getFormat().getFont().setName("Aptos");
    entireRange.getFormat().getFont().setSize(10);


     

    // Check if all entries in "Comment" column are empty
    const allCommentsEmpty = allDataRows.every(row => {
    const val = row[commentColIndex];
    return val === null || val === undefined || val.toString().trim() === "";
    });
     

     

    const tableName = "Table1";
    let targetTable: ExcelScript.Table | undefined;
     

     

    try {
     

     

    targetTable = workbook.getTable(tableName);
    } catch {
    targetTable = undefined;
    }
    const headerNames = targetTable.getHeaderRowRange().getValues()[0] as string[];
    if (targetTable) {
    targetTable.convertToRange();
    }
     

     

    if (allCommentsEmpty) {
     

     

    if (targetTable) {
     

     

    if (headerNames.includes("Comment")) {
    // You can proceed with your logic here
    // e.g., convertToRange or delete column logic
    const colToDelete = commentColIndex;
    const usedRange = sheet.getUsedRange();
    sheet.getRangeByIndexes(0, colToDelete, usedRange.getRowCount(), 1).delete(ExcelScript.DeleteShiftDirection.left);
    }
    }
    }
     

     

    //From here
    const updatedUsedRange = sheet.getUsedRange();
    const updatedValues = updatedUsedRange.getValues();
     

     

    const updatedHeaders = updatedValues[startRow] as string[];
    const updatedProjectColIndex = updatedHeaders.indexOf("Project");
    const updatedActivityColIndex = updatedHeaders.indexOf("Activity");
    const updatedDurationColIndex = updatedHeaders.indexOf("Duration (Hours)");
     

     

    for (let i = startRow + 1; i < updatedValues.length; i++) {
    const row = updatedValues[i];
    const project = row[updatedProjectColIndex]?.toString().trim();
     

     

    if (project === "Weekend") {
    const rowIndex = i;
     

     

    // Define range for Activity + Duration columns
    const rangeToMerge = sheet.getRangeByIndexes(rowIndex, updatedActivityColIndex, 1, 2);
     

     

    // Merge and format
    rangeToMerge.merge(true); // true = merge and center
    rangeToMerge.getFormat().setHorizontalAlignment(ExcelScript.HorizontalAlignment.center);
     

     

    // Set merged value to the activity's original value
    const value = row[updatedActivityColIndex];
    sheet.getCell(rowIndex, updatedActivityColIndex).setValue(value);
    }
    }
     

     

    const refreshedRange = sheet.getUsedRange();
    const refreshedValues = refreshedRange.getValues();
     

     

    // Recalculate column indexes after possible structural changes
    const refreshedHeaders = refreshedValues[startRow] as string[];
    const refreshedProjectColIndex = refreshedHeaders.indexOf("Project");
    const refreshedActivityColIndex = refreshedHeaders.indexOf("Activity");
    const refreshedDurationColIndex = refreshedHeaders.indexOf("Duration (Hours)");
     

     

    for (let i = startRow + 1; i < refreshedValues.length; i++) {
    const row = refreshedValues[i];
    const activity = row[refreshedActivityColIndex]?.toString().trim();
     

     

    if (activity === "Comp Off Leave") {
    const projectCell = sheet.getCell(i, refreshedProjectColIndex);
    projectCell.getFormat().getFill().setColor("yellow");
    }
    }
     

     

    // Merge Project + Activity + Duration for 'Leave' and 'Public Holiday'
    for (let i = startRow + 1; i < refreshedValues.length; i++) {
    const row = refreshedValues[i];
    const project = row[refreshedProjectColIndex]?.toString().trim();
     

     

    if (project === "Leave" || project === "Public Holiday") {
    const rowIndex = i;
     

     

    // Merge the 3 columns starting from Project
    const rangeToMerge = sheet.getRangeByIndexes(rowIndex, refreshedProjectColIndex, 1, 3);
    rangeToMerge.merge(true); // Merge and center
    rangeToMerge.getFormat().setHorizontalAlignment(ExcelScript.HorizontalAlignment.center);
     

     

    // Set merged value to what's in the Activity column (or adjust if needed)
    const value = row[refreshedActivityColIndex];
    sheet.getCell(rowIndex, refreshedProjectColIndex).setValue(value);
    }
    }
     

     

    // Refresh the used range after all changes (e.g., after deleting 'Comment' column and merging)
    const finalUsedRange: ExcelScript.Range = sheet.getUsedRange();
    const finalRowCount: number = finalUsedRange.getRowCount();
    const finalLastColIndex: number = finalUsedRange.getColumnCount() - 1;
     

     

    const lastColumn: ExcelScript.Range = finalUsedRange.getLastColumn();
     

     

    // 2. Get the array of border objects for the entire last column range.
    // The editor understands this as returning a `RangeBorder[]`.
    const borders: ExcelScript.RangeBorder[] = lastColumn.getFormat().getBorders();
     

     

    // 3. Loop through the borders in the array (this loop will only run a few times).
    for (const border of borders) {
    // 4. Check if the current border is the one for the right edge.
    if (border.getSideIndex() === ExcelScript.BorderIndex.edgeRight) {
     

     

    // 5. Apply the formatting.
    border.setStyle(ExcelScript.BorderLineStyle.continuous);
    border.setWeight(ExcelScript.BorderWeight.thick);
    border.setColor("black");
     

     

    // 6. Exit the loop since we've found and formatted the border we need.
    break;
    }
    }
     

     

    // Highlight rows with 'Half Day' in the Comment column as yellow (if the column exists)
    // Highlight only the 'Comment' cell with 'Half Day' as yellow background (if the column exists)
    if (refreshedHeaders.includes("Comment")) {
    const refreshedCommentColIndex = refreshedHeaders.indexOf("Comment");
     

     

    for (let i = startRow + 1; i < refreshedValues.length; i++) {
    const row = refreshedValues[i];
    const comment = row[refreshedCommentColIndex]?.toString().trim();
     

     

    if (comment === "Half Day" || comment === "Half day") {
    const commentCell = sheet.getCell(i, refreshedCommentColIndex);
    commentCell.getFormat().getFill().setColor("yellow");
    }
    }
    }
     




     

    }
     

     

    // Helper function to parse DD-MM-YYYY date strings
    function parseDateString(dateStr: string | undefined): Date {
    if (!dateStr) return new Date(NaN);
    const parts = dateStr.split('-');
    if (parts.length !== 3) return new Date(NaN);
    const day = parseInt(parts[0], 10);
    const month = parseInt(parts[1], 10) - 1; // JavaScript months are 0-based
    const year = parseInt(parts[2], 10);
    return new Date(year, month, day);
    }
     

     

    // Helper function to format date as DD-MM-YYYY
    function formatDate(date: Date): string {
    return `${date.getDate().toString().padStart(2, '0')}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getFullYear()}`;
    }


     

  • MarkRahn Profile Picture
    1,231 Super User 2025 Season 2 on at
    Hi,
     
    Sorry it has taken some time to get back to you.
     
    For the Action that shows a Bad Request Error, click on the Output to see if the JSON contains the actual error in it. Post that if you can (remove any Personal Info first).
     
    How is this flow run? For example, when it runs it creates 50 files at a time? Is it an "Apply for Each " Loop?
    If it is a "Apply For Each", what is the Concurrency set to? Try setting it to 1.
     
    If it seems to be a Timing thing, try adding a Delay Action.
    You should not need to do any of these things but this might help with troubleshooting.
     
    Let me know what you find. Hopefully we can get this solved.
     
    This community is supported by individuals freely devoting their time to answer questions and provide support. They do it to let you know you are not alone. This is a community.

    If someone has been able to answer your questions or solve your problem, please click Does this answer your question. This will help others who have the same question find a solution quickly via the forum search.

    If someone was able to provide you with more information that moved you closer to a solution, throw them a Like. It might make their day. 😊

    Thanks
    -Mark

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 519 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 296 Moderator

#3
abm abm Profile Picture

abm abm 232 Most Valuable Professional

Last 30 days Overall leaderboard