updateCell method have slow performance

 avatar
unknown
typescript
a year ago
2.0 kB
9
Indexable
const handleResetSheet = () => {
      // Collect all the updates

      const updates: any[] = [];



      for (let i = 6; i <= 7; i++) {

          for (let j = 6; j <= 8; j++) {

              collectRangeUpdates(`F${i}:H${i}`, customGetCellValue(`C${j}`), updates);

          }

      }



      for (let i = 12; i <= 17; i++) {

          for (let j = 12; j <= 18; j++) {

              collectRangeUpdates(`F${i}:H${i}`, customGetCellValue(`C${j}`), updates);

          }

      }



      for (let i = 19; i <= 26; i++) {

          for (let j = 19; j <= 27; j++) {

              collectRangeUpdates(`F${i}:H${i}`, customGetCellValue(`C${j}`), updates);

          }

      }



      // Apply all the collected updates in a batch

      applyUpdates(updates);

  };



  const collectRangeUpdates = (range: string, value: string, updates: any[]) => {

      const [start, end] = range.split(':');

      const startIndexes = getCellIndexes(start);

      const endIndexes = getCellIndexes(end);



      for (let row = startIndexes[0]; row <= endIndexes[0]; row++) {

          for (let col = startIndexes[1]; col <= endIndexes[1]; col++) {

              updates.push({ row, col, value });

          }

      }

  };



  const applyUpdates = (updates: any[]) => {

      const activeSheet = spreadsheet.getActiveSheet();

      console.log("draw", updates)



      updates.forEach(({ row, col, value }) => {

          console.log("update", {row, col, value})

          const cell = getCell(row, col, activeSheet);

          const cellAddress = getCellAddress(row, col);



          spreadsheet.updateCell({ ...cell, value }, cellAddress);

      });

  };



  const customGetCellValue = (cell: string | number): string => {

      if (typeof cell === 'number') cell = cell.toString();

      const cellIndexes = getCellIndexes(cell);

      return getCell(cellIndexes[0], cellIndexes[1], spreadsheet.getActiveSheet()).value as string;

  };

Editor is loading...
Leave a Comment