4

I have written a very simple code on my Google Sheets file. This is the purpose:

  1. Save some cells values from StaticSheet (all the Copyxxx) that need to be copied in DynamicSheet.
  2. Get the value of one specific cell inserted by the user manually.
  3. Enter a While loop useful only to increase an indicator and get the number of the row where I want to copy those values previously saved.
  4. Copy those values on this row but different columns.

The problem is that it seems that most of the time it does not even run the script after I told it to do so. What is funny is that sometimes it works, super slowly, but it works for like a couple of minutes. And after it stops working again.

Could you please tell me what am I missing here please?

function Copy_Static_on_Dynamic() {
  var app = SpreadsheetApp;
  var ss = app.openById("xxxxyy--------yyzzzz")
  var StaticSheet = ss.getSheetByName("DEAT Price");
  var DynamicSheet = ss.getSheetByName("DEAT Price + TEST");
  var CopySKU = StaticSheet.getRange(5,1,40);
  var CopyPrices = StaticSheet.getRange(5,3,40,4);
  var CopyUsage = StaticSheet.getRange(5,8,40);
  var Week_1 = StaticSheet.getRange(2,4).getValues();
  var i = 1;
  Logger.clear();
  while(DynamicSheet.getRange(i,3).getValues() != Week_1)
  {
   Logger.log(i);
    i+=1;
  }
  CopySKU.copyTo(DynamicSheet.getRange(i,4,40));
  CopyPrices.copyTo(DynamicSheet.getRange(i,6,40,4));
  CopyUsage.copyTo(DynamicSheet.getRange(i,11,40));
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
Andrea
  • 71
  • 1
  • 5
  • Possible duplicate of [Google app script timeout ~ 5 minutes?](https://stackoverflow.com/questions/14450819/google-app-script-timeout-5-minutes) – TheMaster Dec 05 '18 at 12:10

3 Answers3

6

If you see the "Preparing for Execution" message in the Apps Script editor, you can reload the browser window and run the function again. The program will likely go away.

Amit Agarwal
  • 10,910
  • 1
  • 32
  • 43
  • 1
    That doesn't solve the issue apparently. At the moment on 'Executions' section I can see it is "Running", but is doing it endlessly. Moreover, if I try to insert any break-point for debugging it never reach it, even if I insert it on the second line of the code... :( – Andrea Dec 05 '18 at 13:50
  • The message you see comes because your script is running for too long. You probably tried to manually cancel it's execution but the server is still running for you on multiple instances.At some point it becomes impossible to run any script, you can only wait, sometimes for several hours. I know that it seems not logical because of the 6 minutes limitation but I know it from experience. – Serge insas Dec 05 '18 at 17:57
3

So I think I have solved it. As Serge insas was saying I had my script running on the background, I found it out on the "Execution" section, where you can also interrupt them. After I discover it I kept testing, and I saw that the while loop needed almost 2 seconds to check the condition every time, making the script incredibly long. So instead of:

while(DynamicSheet.getRange(i,3).getValues() != Week_1)

... I have created a variable declared previously such as:

var WeekLOOP = DynamicSheet.getRange(i,3).getValues();
while(WeekLOOP != Week_1) { --- }

... and now the script needs few milliseconds to run the condition. I don't have enough technical knowledge to say if this was the only issue, but is what apparently solved my problem. Thanks to all for the support! Will come back if I need any further help :)

Andrea
  • 71
  • 1
  • 5
0

As was mentioned by Amit Agarwal, to solve the error message mentioned on the question, refresh the web browser tab.

Regarding the code,

On

var Week_1 = StaticSheet.getRange(2,4).getValues();

and

DynamicSheet.getRange(i,3).getValues()

instead of getValues you should use getValue because your code are referring to single cell cells otherwise you will be getting 2D arrays instead of scalar values.

The use of while should be made very carefully to avoid functions running endlessly. You could add some "safeguard" like the following

var max_iterations = 100 // Edit this while(DynamicSheet.getRange(i,3).getValue() != Week_1 && i <= max_iterations) {

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • I swear there was getValue until yesterday, I just changed because I found on internet that getValues was for string while getValue for date. It was just a try, I don't really know if it makes difference. On the second condition should I just check the value of " i " variable instead of the value of the cell? – Andrea Dec 05 '18 at 16:39
  • It makes difference. Regarding the second condition, you are right. – Rubén Dec 05 '18 at 16:47