function main(workbook: ExcelScript.Workbook): { base64Image: string } {
  const sheet = workbook.getWorksheet("Snapshot");
  const protection = sheet.getProtection();

  if (protection.getProtected() && !protection.getIsPaused()) {
    protection.pauseProtection();
  }

  let dataRange = sheet.getUsedRange();
  // Ensure dataRange is always valid. If sheet is empty, getUsedRange() returns null.
  if (!dataRange) {
    dataRange = sheet.getRange("A1");
  }

  const allValues = dataRange.getValues();
  const rowCount = allValues.length;
  // Ensure colCount is at least 1 even if the sheet is technically empty (A1 fallback).
  const colCount: number = allValues[0] ? allValues[0].length : 1;

  // STEP 1: Define target cells and their original formats (B5-E5, B10-E10)
  interface SavedFormat {
    horizontalAlignment: ExcelScript.HorizontalAlignment;
    verticalAlignment: ExcelScript.VerticalAlignment;
    wrapText: boolean;
    shrinkToFit: boolean;
    fontName: string;
    fontSize: number;
    fontBold: boolean;
    fontItalic: boolean;
    fontColor: string;
    fillColor: string;
  }

  interface CellRef {
    row: number; // 0-indexed row
    col: number; // 0-indexed column
  }

  const targetCells: CellRef[] = [
    { row: 4, col: 1 }, // B5 (0-indexed: row 4, col 1)
    { row: 4, col: 2 }, // C5
    { row: 4, col: 3 }, // D5
    { row: 4, col: 4 }, // E5
    { row: 9, col: 1 }, // B10 (0-indexed: row 9, col 1)
    { row: 9, col: 2 }, // C10
    { row: 9, col: 3 }, // D10
    { row: 9, col: 4 }  // E10
  ];

  const originalFormats: SavedFormat[] = [];

  // Save formatting for target cells only.
  // Reading properties (even chained) is generally safe and doesn't trigger aliasing issues.
  for (let i = 0; i < targetCells.length; i++) {
    const cellRef = targetCells[i];
    const cell = sheet.getRangeByIndexes(cellRef.row, cellRef.col, 1, 1);
    const format = cell.getFormat();

    originalFormats[i] = {
      horizontalAlignment: format.getHorizontalAlignment(),
      verticalAlignment: format.getVerticalAlignment(),
      wrapText: format.getWrapText(),
      shrinkToFit: format.getShrinkToFit(),
      fontName: format.getFont().getName(),
      fontSize: format.getFont().getSize(),
      fontBold: format.getFont().getBold(),
      fontItalic: format.getFont().getItalic(),
      fontColor: format.getFont().getColor(),
      fillColor: format.getFill().getColor()
    };
  }

  // STEP 2: Excel Web optimized column widths - precise calculation based on content length
  for (let col = 0; col < colCount; col++) {
    let maxContentLength = 0;

    // Quick pass through column data to find max content length
    for (let row = 0; row < rowCount; row++) {
      const value = allValues[row][col];
      if (value) {
        const length = value.toString().length;
        if (length > maxContentLength) {
          maxContentLength = length;
        }
      }
    }

    // Excel Web optimized width calculation (browser-friendly units).
    // These values are tuned for general readability and image capture.
    let columnWidth: number;
    if (maxContentLength <= 8) {
      columnWidth = 60; // Minimum readable width for screenshot
    } else if (maxContentLength <= 20) {
      columnWidth = 90; // Standard text width
    } else if (maxContentLength <= 50) {
      columnWidth = 150; // Longer content
    } else {
      columnWidth = 200; // Maximum practical width to avoid excessive size
    }

    // Apply width - single operation per column.
    // Chaining getFormat().setColumnWidth() is compliant.
    const colRange = sheet.getRangeByIndexes(dataRange.getRowIndex(), dataRange.getColumnIndex() + col, rowCount, 1);
    colRange.getFormat().setColumnWidth(columnWidth);
  }

  // STEP 3: Excel Web optimized row heights - precise calculation based on line breaks/long content
  for (let row = 0; row < rowCount; row++) {
    let hasLineBreaks = false;
    let hasLongContent = false;

    // Quick check for content type
    for (let col = 0; col < colCount; col++) {
      const value = allValues[row][col];
      if (value) {
        const text = value.toString();
        if (text.includes('\n')) {
          hasLineBreaks = true;
          // No need to check other cells in this row for line breaks once found
          break;
        }
        if (text.length > 40) { // Arbitrary threshold for "long" single-line content
          hasLongContent = true;
        }
      }
    }

    // Excel Web friendly heights (optimized for browser rendering and image capture).
    // These values are tuned for general readability and image capture,
    // ensuring enough space for wrapped text.
    let rowHeight: number;
    if (hasLineBreaks) {
      rowHeight = 45; // Multi-line content (more generous for screenshot)
    } else if (hasLongContent) {
      rowHeight = 30; // Long single-line content (may wrap)
    } else {
      rowHeight = 20; // Standard single-line height for Excel Web
    }

    // Apply height - single operation per row.
    // Chaining getFormat().setRowHeight() is compliant.
    const rowRange = sheet.getRangeByIndexes(dataRange.getRowIndex() + row, dataRange.getColumnIndex(), 1, colCount);
    rowRange.getFormat().setRowHeight(rowHeight);
  }

  // STEP 4: Apply general formatting to the entire used range
  const mainRangeFormat = dataRange.getFormat();
  mainRangeFormat.setWrapText(true);
  mainRangeFormat.setVerticalAlignment(ExcelScript.VerticalAlignment.center);
  mainRangeFormat.setHorizontalAlignment(ExcelScript.HorizontalAlignment.center); // Apply globally for general consistency

  // Set consistent font for the entire range
  // CRITICAL FIX FOR EXCEL WEB: Assign to a new, unique temporary variable for EACH setter.
  const mainFontNameSetter = dataRange.getFormat().getFont();
  mainFontNameSetter.setName("Calibri");
  const mainFontSizeSetter = dataRange.getFormat().getFont();
  mainFontSizeSetter.setSize(11);
  const mainFontBoldSetter = dataRange.getFormat().getFont();
  mainFontBoldSetter.setBold(false); // Ensure not bold by default

  // STEP 5: Apply specific formatting for target cells (B5-E5, B10-E10)
  for (let i = 0; i < targetCells.length; i++) {
    const cellRef = targetCells[i];
    const cell = sheet.getRangeByIndexes(cellRef.row, cellRef.col, 1, 1);

    // Clear existing formatting (important before applying new)
    cell.clear(ExcelScript.ClearApplyTo.formats);

    // Apply Excel Web optimized formatting for specific cells.
    // Direct setters on cell.getFormat() are compliant.
    cell.getFormat().setHorizontalAlignment(ExcelScript.HorizontalAlignment.center);
    cell.getFormat().setVerticalAlignment(ExcelScript.VerticalAlignment.center);
    cell.getFormat().setWrapText(true);
    cell.getFormat().setShrinkToFit(false); // ShrinkToFit can cause readability issues in images

    // CRITICAL FIX FOR EXCEL WEB: Assign to a new, unique temporary variable for EACH setter on Font object.
    const targetFontNameSetter = cell.getFormat().getFont();
    targetFontNameSetter.setName("Calibri");
    const targetFontSizeSetter = cell.getFormat().getFont();
    targetFontSizeSetter.setSize(11);
    const targetFontBoldSetter = cell.getFormat().getFont();
    targetFontBoldSetter.setBold(true); // Make target cells bold for distinction, adjust as needed
    const targetFontItalicSetter = cell.getFormat().getFont();
    targetFontItalicSetter.setItalic(false);
    const targetFontColorSetter = cell.getFormat().getFont();
    targetFontColorSetter.setColor("black"); // Ensure a clear font color

    // CRITICAL FIX FOR EXCEL WEB: Assign to a new, unique temporary variable for EACH setter on Fill object.
    const targetFillColorSetter = cell.getFormat().getFill();
    targetFillColorSetter.setColor("noFill");
  }

  // STEP 6: Calculate the workbook
  // Recalculate is usually sufficient and faster for web.
  const app = workbook.getApplication();
  app.calculate(ExcelScript.CalculationType.recalculate);

  // STEP 7: Capture image with error handling
  let base64Image: string;
  try {
    base64Image = dataRange.getImage();
  } catch (error) {
    // If first attempt fails (common in web scripts due to rendering timing),
    // force another recalculation and retry.
    app.calculate(ExcelScript.CalculationType.recalculate);
    base64Image = dataRange.getImage();
  }

  // STEP 8: Restore original formatting for target cells
  for (let i = 0; i < targetCells.length; i++) {
    const cellRef = targetCells[i];
    const cell = sheet.getRangeByIndexes(cellRef.row, cellRef.col, 1, 1);
    const originalFormat = originalFormats[i];

    // Direct setters on cell.getFormat() are compliant.
    cell.getFormat().setHorizontalAlignment(originalFormat.horizontalAlignment);
    cell.getFormat().setVerticalAlignment(originalFormat.verticalAlignment);
    cell.getFormat().setWrapText(originalFormat.wrapText);
    cell.getFormat().setShrinkToFit(originalFormat.shrinkToFit);

    // CRITICAL FIX FOR EXCEL WEB: Assign to a new, unique temporary variable for EACH setter on Font object.
    const restoreFontNameSetter = cell.getFormat().getFont();
    restoreFontNameSetter.setName(originalFormat.fontName);
    const restoreFontSizeSetter = cell.getFormat().getFont();
    restoreFontSizeSetter.setSize(originalFormat.fontSize);
    const restoreFontBoldSetter = cell.getFormat().getFont();
    restoreFontBoldSetter.setBold(originalFormat.fontBold);
    const restoreFontItalicSetter = cell.getFormat().getFont();
    restoreFontItalicSetter.setItalic(originalFormat.fontItalic);
    const restoreFontColorSetter = cell.getFormat().getFont();
    restoreFontColorSetter.setColor(originalFormat.fontColor);

    // CRITICAL FIX FOR EXCEL WEB: Assign to a new, unique temporary variable for EACH setter on Fill object.
    const restoreFillColorSetter = cell.getFormat().getFill();
    restoreFillColorSetter.setColor(originalFormat.fillColor);
  }

  // Final recalculation before finishing script execution.
  app.calculate(ExcelScript.CalculationType.recalculate);

  if (protection.getIsPaused()) {
    protection.resumeProtection();
  }

  return { base64Image: `data:image/png;base64,${base64Image}` };
}

Please fix this for Excel WEB which does not support office scripts apis