1

I want to search for a word in a certain column and then if the value exists, I want to copy the row below with its values and change the word to two different words. My issue was in getting the found word row Number to insert a row below it.

function myFunction() {
var ss = SpreadsheetApp.openById("1fE404JUbw3aytlqtoht6FfrIhYhTpSe2MM5UDnBkFc4");
var sheet = ss.getSheets()[0]
let range = sheet.getDataRange();
var values = range.getValues();
var typeRange = sheet.getRange("E2:E");
var typeValues = typeRange.getValues();

  var i;
  for(i = 0; i <= lastRow ; i++){
    for (let type of typeValues){
      if (type == "Both"){
        var bothRow = i+1;
        
      }
////      var bothRow = sheet.getRange(i+1,1,1,typeValues[i].length);
////      ss.insertRowsAfter(bothRow, 1);
    }
  }
}

I have used alert to check and it inserted an infinite number of rows after row number 1.

function myFunction() {
  
  let sheetui= SpreadsheetApp.getUi()
  sheetui.createMenu("Rahaf Game")
  .addItem("Stage 1", 'pop')
  .addToUi();
  
//  var ss = SpreadsheetApp.openById("1fE404JUbw3aytlqtoht6FfrIhYhTpSe2MM5UDnBkFc4");
//  var sheet = ss.getSheets()[0]
//  let range = sheet.getDataRange();
//  var values = range.getValues();
//  var typeRange = sheet.getRange("E2:E");
//  var typeValues = typeRange.getValues();
}
  function pop(){
  //var ss = SpreadsheetApp.openById("1fE404JUbw3aytlqtoht6FfrIhYhTpSe2MM5UDnBkFc4");
  var sheet = ss.getSheets()[0]
  var typeRange = sheet.getRange("E2:E");
  var typeValues = typeRange.getValues();
  var lastRow = sheet.getLastRow();
  var i;
  for(i = 0; i <= lastRow ; i++){
    for (let type of typeValues){
      if (type == "Both"){
        var bothRow = i+1;
        ss.insertRowsAfter(bothRow, 1);
      }
////      var bothRow = sheet.getRange(i+1,1,1,typeValues[i].length);
////      ss.insertRowsAfter(bothRow, 1);
    }
  }
}

Can someone please help in achieving the required result in inserting a row below and copy the values into it with changing the word into two words?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
RAHAF
  • 23
  • 5

1 Answers1

0

Answer:

.getValues() returns a 2D array of values and you are referencing it as if it is unidimentional.

Code fix:

You need to change your conditional such that the object you are checking is an array:

if (type == "Both"){
//code
}

Should be:

if (type == ["Both"]){
//code
}

Adding a row and copying the data to it:

You can add a row in a sheet after a given row with the insertRowsAfter() and use .getRange().setValues() to copy in the data:

for(var i = 0; i <= lastRow ; i++){
  for (let type of typeValues){
    if (type == ["Both"]){
      var bothRow = i + 1;
      ss.insertRowsAfter(bothRow, 1);
      // increment lastRow as there is now one more row in the sheet
      lastRow++;
      // increment i as you want to skip the row you just copied!
      i++;
      var rangeToCopy = sheet.getRange(bothRow + ':' + bothRow).getValues();
      sheet.getRange((bothRow + 1) + ':' + (bothRow + 1)).setValues(rangeToCopy);
    }
  }
}

Don't forget to increment lastRow by 1 if you add a new row so that your loop still reaches the end of the sheet.

References:

Related Questions:

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54