简体   繁体   中英

Need to merge cells in Google Sheets from value in another cell

I have a spreadsheet with a lot of base data. using =filter() I pull out just what I want to report on into the 2nd tab. I have the data broken down into 1/4 a month so that columns b:e are Jan, F:I are Feb, and so on. =filter is pulling in 3 reports with a random number of rows per report. so report 1 could be rows 2-10, report 2 could be rows 11-18, report 3 could be row 19-40. At the beginning of each report is a header row of Jan, Feb, Mar. I want to merge the 4 columns in the header row where the months are so that the month title spans the 4 quarters of the month. Since we won't know which rows this will be, I am guessing it needs to be a script that looks for "header" in column A but then merges B:E, F:I for just the header line.

I will end up with multiple tabs with different =filter() formulas but have the same header info in the same columns.

I am looking into using a script with mergeAcross() but cannot figure out the syntax to target just the row with Header and have it merge the 4 rows for the months.

最终结果的样本

Here is a working attempt at this. I added some columns so now my dates start at column H instead of Column D

function mergingMonth(){

  var ss = SpreadsheetApp.getActive();
  var lastRow = ss.getLastRow();
  var range = ss.getRange("A1:A" + lastRow);
  var data = range.getValues();

  for (var r = 0; r < data.length; r++) {
    for (var c = 0; c < 1; c++)
      if (data[r][c] == "header") {
      var row = r+1
          ss.getRange("H"+row+":K"+row).mergeAcross();
          ss.getRange("L"+row+":O"+row).mergeAcross();
          ss.getRange("P"+row+":S"+row).mergeAcross();
          ss.getRange("T"+row+":W"+row).mergeAcross();
          ss.getRange("X"+row+":AA"+row).mergeAcross();
          ss.getRange("AB"+row+":AE"+row).mergeAcross();
          ss.getRange("AF"+row+":AI"+row).mergeAcross();
          ss.getRange("AJ"+row+":AM"+row).mergeAcross();
          ss.getRange("AN"+row+":AQ"+row).mergeAcross();
          ss.getRange("AR"+row+":AU"+row).mergeAcross();
          ss.getRange("AV"+row+":AY"+row).mergeAcross();
          ss.getRange("AZ"+row+":BC"+row).mergeAcross();
          ss.getRange("BD"+row+":BG"+row).mergeAcross();
          ss.getRange("BH"+row+":BK"+row).mergeAcross();
          ss.getRange("BL"+row+":BO"+row).mergeAcross();
      }
  }  
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM