0

I would like to add rows based on a specific cell value yet that the rows will be added only below (or at) cells that answer a certain condition.

i tried to search Google but all i found was how to add rows after every other row.

function insertBlankRows() {
  // version 1.1, written by --Hyde, 17 January 2023
  //  - see https://webapps.stackexchange.com/q/168794/269219
  const range = SpreadsheetApp.getActiveSheet().getRange('X2:X');
  const rowStart = range.getRow();
  const rowNumbers = [];
  const numRowsToInsert = range.getValues()
    .flat()
    .filter((value, index) => {
      if (typeof value === 'number' && value > 1) {
        rowNumbers.push(rowStart + index);
        return true;
      }
      return false;
    })
    .map(numRows => numRows - 1);
  insertBlankRows_(range.getSheet(), rowNumbers, numRowsToInsert);
}


/**
* Inserts numRowsToInsert blank rows in sheet after rowNumbers.
*
* @param {Sheet} sheet The sheet where to insert blank rows.
* @param {Number[]} rowNumbers The 1-indexed row numbers after which to insert blank rows.
* @param {Number[]} numRowsToInsert How many rows to insert after each rowNumbers.
*/
function insertBlankRows_(sheet, rowNumbers, numRowsToInsert) {
  // version 1.0, written by --Hyde, 8 July 2021
  //  - see https://support.google.com/docs/thread/116344628?msgid=116435373
  numRowsToInsert.reduceRight((_, numRows, index, array) => {
    sheet.insertRowsAfter(rowNumbers[index], numRows);
  }, null);
}

when i want to create something that will add rows only after specific text values (list).

for example:

Column A Column B
dadada 123456
dadada 123456
bababa 123456

i would like based on a certain cell that when the value in column A changes add rows there.

Column A Column B
dadada 123456
dadada 123456
bababa 123456
bababa 123456

enter image description here

  • I have to apologize for my poor English skill. Unfortunately, I cannot understand `i would like based on a certain cell that when the value in column A changes add rows there.`. Can I ask you about the detail of your current issue and your goal? – Tanaike Jun 08 '23 at 11:53
  • I added an image that might make it clearer – opal tabor Jun 08 '23 at 12:29
  • Thank you for replying. I have to apologize for my poor English skill, again. Unfortunately, I cannot understand the relationship between the 2 tables in your question and your provided image. Can I ask you about the detail of it? – Tanaike Jun 08 '23 at 12:40
  • Would you please clarify... In the upper table, there are three rows: Column A="dadada" in rows 1&2, "bababa" in row 3. This translates to the lower table: rows 1&2 = unchanged, row3 is inserted blank, row4&5="bababa". Now to the example image: the left side translates to the right side with three rows inserted between each category, and the number of rows = the highlighted cell. However... 1) the logic in the upper and lower table (inserting blank row) seems different to the example image (no blank row). 2) are these results to be dynamic (created by formula) or permanent (created by script)? – Tedinoz Jun 10 '23 at 00:48
  • dynamic. I think the image is a better example. – opal tabor Jun 11 '23 at 05:21

1 Answers1

0

Try this formula:

=query({A2:B6;INDEX(SPLIT(FLATTEN(SPLIT(JOIN("",INDEX(REPT(BYROW({A2:A6,C2:C6},LAMBDA(x,TEXTJOIN("|",0,x)))&" @",$E$1))),"@")),"|"))},"select Col1, Col2 order by Col1")

Based on the answer by @Harun24hr in Repeat whole row N times based on column value in Google Sheets


Sample output

image1

Tedinoz
  • 5,911
  • 3
  • 25
  • 35