Copy/Paste from one spreadsheet to another

Posted by: cody.westlund on 21 July 2023, 5:29 pm EST

  • Posted 21 July 2023, 5:29 pm EST

    Hello,

    We are attempting to copy data from cells (formulas/formatting/values) from one SpreadJS instance to another SpreadJS instance. In the “destination” SpreadJS instance where the focus is, we have our “source” SpreadJS worksheet that’s been imported via json. We’d like to programmatically copy a range from the source to the destination worksheet.

    Here is what we have tried:

    destinationWorkBook.commandManager().execute({
    cmd: "clipboardPaste",
    sheetName: sourceSheet.name(),
    fromSheet: sourceSheet,
    fromRanges: fromRange,
    pastedRanges: toRange,
    isCutting: false,
    clipboardText: "",
    pasteOption: GC.Spread.Sheets.ClipboardPasteOptions.all
    });

    So far nothing is pasted.

    Thanks,

    Cody

  • Posted 24 July 2023, 7:06 am EST

    Hi,

    we are sorry but external copy-paste is not supported in spreadJS, If you want to move some cells to another sheet you need to move by using APi. Please refer to the following code snippet and attached sample that explains the same.

    function copyRange(options) {
      let {
        desSpread,
        sourceSpread,
        desSheetName,
        sourceSheetName,
        sourceRange,
        desStartRow,
        desStartCOl
      } = options;
    
      let sSheet = sourceSpread.getSheetFromName(sourceSheetName);
      let dSheet = desSpread.getSheetFromName(desSheetName);
      sSheet.suspendPaint();
    
      for (
        let i = sourceRange.row, k = desStartRow;
        i < sourceRange.row + sourceRange.rowCount;
        i++, k++
      ) {
        for (
          let j = sourceRange.col, l = desStartCOl;
          j < sourceRange.col + sourceRange.colCount;
          j++, l++
        ) {
          let sCell = sSheet.getCell(i, j);
          let dCell = dSheet.getCell(k, l);
          // dCell.fromJSON(sCell.toJSON());
          dCell.value(sCell.value());
    
          //if cell has formula
    
          if (sCell.formula()) {
            dCell.formula(sCell.formula());
          }
    
          //clone the style
          dSheet.setStyle(k, l, sSheet.getStyle(i, j));
        }
      }
    
      sSheet.resumePaint();
    }
    

    sample: https://codesandbox.io/s/tender-neumann-43gpp2?file=/src/index.js:996-1946

    Regards,

    Avinash

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels