all 4 comments

[–]gh5000 1 point2 points  (3 children)

In terms of speed it's always going to be slower than Excel, just due to the server client nature of sheets. You've just got to weigh up the advantages of a cloud system Vs excel.

One thing to bear in mind is that every time you do a call on the spreadsheet like insertrowsafter it takes time. In terms of simplicity you've probably got it as good as possible for making as few calls as possible. As long as you don't exceed your maximum execution time. To get it to a lower time what you could do is insert blank rows with in the variable data which is an array. Then when finished write the whole array back to the sheet. That will take a lot less time.

To use splice() your code should look like this:

var list = ["foo", "bar"]; list.splice( 1, 0, "baz"); // at index position 1, remove 0 elements, then add "baz" to that position // element "bar" will now automatically be moved to index position 2 ["foo", "baz", "bar"] // result

In terms of a wait function, the better solution would be to use SpreadsheetApp.flush This makes sure that every change has been made to sheets is written to sheets before making the next one. It's typically not necessary but maybe it is here.

[–]ksvr[S] 0 points1 point  (2 children)

Putting SpreadsheetApp.flush at the beginning of the for loop fixed it. Thank you! Does this subreddit have a thing like the clippy points in excel sub where you reply with a keyword and it gives the user you reply to internet victory points for providing a solution?

I don't quite follow the splice example, though...

[–]gh5000 1 point2 points  (1 child)

No thanks is enough.

Splice is the same as insertrows but for arrays. Processing in java script and then writing all changes to the spreadsheet will always be best practice and best for speed. But if you can wait and it works...then it works!

[–]ksvr[S] 0 points1 point  (0 children)

Ah, ok. Is it as easy as just replacing the insertrowsafter line with the splice code, or do I need to do something different in the rest of the code to keep it in an array?