APP SCRIPT // Copying a sheet from a file/spreadsheet to another

48 Views Asked by At

I'd like to copy a sheet from a spreadsheet to another but copyto does not work since they are in different spreadsheet : Error // Exception: Target range and source range must be on the same spreadsheet.

Only things on web seems to copy only values or within the same spreadsheet even when asked between different spreadsheets.

Note that the initial goal is to copy a fractionned file because it is too large (7Mb, 959tabs of not that big charts with formating). But sheets seems to be a nightmare and the code is limited to run for 6 minutes only which is very short when realizing how slow it is. And nothing seems to be achieved localy on machine.

I tried with code found on web, setting each parameter but I cannot get the formulas, the hyperlinks, the borders and the columnHeights are still higher.

a code of this kind :

function CopyTable(srcSheet, destSheet, copyRange){

  destSheet.clear();

  var srcRange = srcSheet.getRange(copyRange); 

  var values = srcRange.getValues();
  var background = srcRange.getBackgrounds();
  var fontColor = srcRange.getFontColors();
  var fontFamily = srcRange.getFontFamilies();
  var fontLine = srcRange.getFontLines();
  var fontSize = srcRange.getFontSizes();
  var fontStyle = srcRange.getFontStyles();
  var fontWeight = srcRange.getFontWeights();
  var textStyle = srcRange.getTextStyles();
  var horAlign = srcRange.getHorizontalAlignments();
  var vertAlign = srcRange.getVerticalAlignments();
  var bandings = srcRange.getBandings();
  var mergedRanges = srcRange.getMergedRanges();

  var destRange = destSheet.getRange(copyRange);
 
  destRange.setValues(values);
  destRange.setBackgrounds(background);
  destRange.setFontColors(fontColor);
  destRange.setFontFamilies(fontFamily);
  destRange.setFontLines(fontLine);
  destRange.setFontSizes(fontSize);
  destRange.setFontStyles(fontStyle);
  destRange.setFontWeights(fontWeight);
  destRange.setTextStyles(textStyle);
  destRange.setHorizontalAlignments(horAlign);
  destRange.setVerticalAlignments(vertAlign);

  var sourceValues = srcRange.getRichTextValues();
  var targetValues = destRange.getRichTextValues();
  for (var i = 0; i < sourceValues.length; i++) {
    for (var j = 0; j < sourceValues[0].length; j++) {
      var sourceLinkUrl = sourceValues[i][j].getLinkUrl();
      
      if (sourceLinkUrl != null) {
        targetValues[i][j] = SpreadsheetApp.newRichTextValue()
          .setText(targetValues[i][j].getText())
          .setLinkUrl(sourceLinkUrl)
          .build();
      }
    }
  }
  
  for (let i in bandings){
    let srcBandA1 = bandings[i].getRange().getA1Notation();
    let destBandRange = destSheet.getRange(srcBandA1);

    destBandRange.applyRowBanding()
    .setFirstRowColor(bandings[i].getFirstRowColor())
    .setSecondRowColor(bandings[i].getSecondRowColor())
    .setHeaderRowColor(bandings[i].getHeaderRowColor())
    .setFooterRowColor(bandings[i].getFooterRowColor());
  }

  for (let i = 0; i < mergedRanges.length; i++) {
    destSheet.getRange(mergedRanges[i].getA1Notation()).merge();
  }
 
  for (let i = 1; i <= srcRange.getWidth(); i++) {
    //Logger.log(i + "    " + srcRange.getHeight());
    let width = srcSheet.getColumnWidth(i);
    destSheet.setColumnWidth(i, width);
  }
 
  //// BEWARE i = 5 to skip top
  for (let i = 1; i <= srcRange.getHeight(); i++){
    let height = srcSheet.getRowHeight(i);
    destSheet.setRowHeight(i, height);
  }
}

Seems dirty and barely not possible at this point, plus authorization seems to be a nightmare in many cases. WTH

Any thoughts ? Thanks

0

There are 0 best solutions below