Home > Software design >  How can I make an array of ranges properly merge in Google Sheets with GAS?
How can I make an array of ranges properly merge in Google Sheets with GAS?

Time:01-10

I am trying to make a cross-spreadsheet importing tool which transfers (or at least pretends to) both data and format from one spreadsheet to another. So far, the thing that has not been working as (I think) it should, is cell merging.

Currently, I am trying to get all merged cells from the source range with .getMergedRanges(), which gives me a 57-element-long array of ranges, which I then loop with .getRange(array[index]).merge(), but when looping it seems to not be able to merge all the ranges properly despite the program actually running through all indexes.

Here is my code at the moment:

function uploadPlan(){

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var s = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  //var ui = SpreadsheetApp.getUi();

  var uploadSheet = ss.getSheetByName('Course Plan');
  var sheetIndex = uploadSheet.getIndex()-1;
  var repoID = ss.getRangeByName('planRepoID').getValue().toString();
  var planRepo = SpreadsheetApp.openById(repoID);
  var uploadStandard = ss.getRangeByName('planUploadStandard').getValue().toString();
  var uploadRange = ss.getRangeByName('planUploadRange');
  var planID = ss.getRangeByName('planIdentifier').getValue();
  planID = planID.split(" ").join("_");
  var downloadRange = planRepo.getRangeByName(planID);

  switch(uploadStandard){

    case 'BAEFCO Standard':

      uploadRange = s.getRange(uploadRange.getRow(), uploadRange.getColumn(), downloadRange.getNumRows(), downloadRange.getNumColumns());
      uploadRange = sh[sheetIndex].getRange(uploadRange.getA1Notation());

      var mergedRanges = downloadRange.getMergedRanges();

      for(var i=0; i<mergedRanges.length; i  ){

        sh[sheetIndex].getRange(mergedRanges[i].getA1Notation()).merge();
        console.log(mergedRanges[i].getA1Notation());
      }

      uploadRange.setBackgrounds(downloadRange.getBackgrounds());

      break;

    default: 

      //ui.alert("Please select a supported Import Standard.");
    
  }

}

I expect it to merge all cells properly, but it goes through some and then stops working.

enter image description here

Here's the code:

function uploadplan(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getActiveSheet();   
  var planRepo = SpreadsheetApp.openById("1Tz5_xPoKAdPBOVGvNByFar7PGmdxOHeyzA2WIZhs1LE")
  var downloadRange = planRepo.getRangeByName("downloadrange");
 //these first lines you should better use the definitions in your own function. What's important is next:

  var backup = s.copyTo(planRepo).setName("Backup") //Is 's' the the sheet where you have the named range?, if not please change it

  var uploadRange = planRepo.getRangeByName("Backup!uploadrange"); //It's important the change of names of the previous step, because that's what will allow you to access the named range. If 'Backup' isn't possible because you're using the name for another sheet, change Backup in this line and the previous one with another temporary value


  uploadRange.copyTo(downloadRange) // This copies everything, included formulas
  uploadRange.copyTo(downloadRange,SpreadsheetApp.CopyPasteType.PASTE_VALUES,false) //If you want you can use this last line if you want to set only values after having copied the formats and merged cells in the previous step
  
  planRepo.deleteSheet(backup) // and you delete that backup sheet

}

  • Related