2

I have a sheet which one column (column 10) is a drop down list with 2 possibilities : official or non official. I'm trying to create a script that export this sheet to another with this condition : Each row with "Official" in the column 10 have to be duplicated as "non official" (so in this case we will have 2 rows one official and one non official)

So this is my code :

const tab = active_sheet.getRange("A2:M14").getValues();
for (let nbline=0; nbline <tab.length;nbline++) {
if (tab[nbline][10] == "official") {

And I don't find the command to duplicate this line, keep all information and just change the column 10 to "non official"

If someone can help me

Thanks

Nabs335
  • 125
  • 8

1 Answers1

2

So you want to keep the line with "official" and add another one that has the same values except it is "non official", right?

Try something like this:

const tab = active_sheet.getRange("A2:M14").getValues();
var result = [];
for (let nbline=0; nbline <tab.length;nbline++) {
  result.push(tab[nbline]);
  if (tab[nbline][10] == "official") {
    tab[nbline][10] = "non official";
    result.push(tab[nbline]);
  }
}
  • Hi,Thanks for your answer but I want to duplicate the line into tab and not another array ... – Nabs335 Apr 17 '22 at 13:54
  • 1
    @RicardoAranguren, I just saw this question and it seems I answered a duplicate one. Anyways, a good approach and similar approach to mine, but if you try and check the result variable after the loop, it will show `non official` on all rows (which I dont understand why since it looks fine and logging the array to be pushed shows the expected result). I have encountered this issue earlier and modified the script a little to fix it. See https://stackoverflow.com/a/71918011/17842569 – NightEye Apr 19 '22 at 04:18
  • You pushed a value and then modify it afterwards, thus containing both non official on the original and duplicated row. You dont create a new array when you push it so if you modify that array after you push it, then it will also reflect on the original line. – NightEye Apr 20 '22 at 02:34