How do I know if spreadsheet cells are merged using google apps script

后端 未结 9 1211
予麋鹿
予麋鹿 2020-12-01 17:10

In a Google docs spreadsheet. If cells A1 & A2 are merged, is there a way to confirm they are merged, using google apps script?

There is a merge function in GAS

相关标签:
9条回答
  • 2020-12-01 17:36

    If anyone here needs a work around for this problem, I wrote a little tool that takes most of the donkey work out of making a key value file of all merged cells from exported HTML.

    You can find it here: https://github.com/martinhbramwell/CellMergeWorkAround

    I wrote it because I have a small project that depends on being able to fully clone ranges from one spreadsheet to another. Obviously, I have added my vote to wanting the issue fixed.

    (Note: I would have added this as a comment to the correct answer, but lack the necessary points.)

    0 讨论(0)
  • 2020-12-01 17:36

    Maybe it's a bit late answer since I didn't see this problem for new version of google spreadsheets. But, I still use old spreadsheets version for my project. So I was needed somehow figure it out.

    I need to copy format of previous column for new columns. This is part of code below:

          // trying to find first column with week day (white background)
          var background = '';
          for(var columnIndex = 3; columnIndex<=columnLetters.length; columnIndex++){
            background = sheet.getRange(1, columnIndex).getBackground();
            if(background == 'white' || background == ''){
              tmpRange = columnLetters[columnIndex]+':'+columnLetters[columnIndex];
              Logger.log('tmpRange: '+tmpRange);
              // workaround to be able to copy format if there is some merged cells
              try{
                Logger.log('try');
                // copy format of previous column
                sheet.getRange(tmpRange).copyTo(
                  sheet.getRange('B1'), {formatOnly:true}
                );
              }catch(e) {
                Logger.log('continue');
                continue;
              }
              Logger.log('break');
              break;
            }
          }
    

    So, the most important part here is:

    try{
      Logger.log('try');
      // copy format of previous column
      sheet.getRange(tmpRange).copyTo(
        sheet.getRange('B1'), {formatOnly:true}
      );
    }catch(e) {
      Logger.log('continue');
      continue;
    }
    

    If it's impossible to copy those cells it just continue to search others.

    0 讨论(0)
  • 2020-12-01 17:39

    For those like me who didn't know the .breakapart() method to unmerge cells: https://developers.google.com/apps-script/class_range#breakApart

    Thanks to Henrique for the tip !

    0 讨论(0)
提交回复
热议问题