1
votes

Hi i have a column G2: G in googlesheet named 'Rank sheet' with storename like column G storeA storeB storeC storeA storeA storeD

I want to create a function name 'remove' (google apps script) to delete row of duplicated names, but when i run it, gsheet say :"TypeError: Cannot read property "length" from undefined." Can anyone can help me to fix it or help with your code. Thank so much My code is:

function remove() {
  var range2 = rankSheet.getRange("G2:G");
  var values2 = range2.getValues();
  for (var n = 0; n < values2.length; n++) {
if(values2[n] == values2[n+1]) {      
  var outarray = []
  outarray.push(n+2)
  Logger.log(outarray)
  }
}
  for (var m = 0; m <= outarray.length - 1; m++) {
  rankSheet.deleteRow(outarray[m]);
  }
}
1
the first thing to do would be to move var outarray = [] above your for loop. instead of adding to it you are are setting it to a new blank array every time you go through the loop.ScampMichael
var range2 = rankSheet.getRange("G2:G"); -- where does rankSheet come from? it is not defined in your function. is it from a global variable? you need to let people know these kind of things when you're asking questions.ScampMichael

1 Answers

-1
votes

That means that in your fourth line, you are trying to access a property called length from an object that doesn't exist. So if you look at line 3, your getValues() call on range2 is returning null

Also keep in mind getValues() returns a two-dimensional array https://developers.google.com/apps-script/reference/spreadsheet/range#getValues()

Try logging out values2 after you assign it and see what it is ^^