1
votes

I'm having this weird problem with my app script. I have two functions, one that processes text to remove line breaks and one that removes multiple spaces. When I manually run the two functions one after the other, the line break is removed and any multiple spaces are then removed where the line break was. But when I make a function like the following, the second function does not have any effect:

function bothTest() {
  cleanText();
  removeMultipleSpaces();
}

Surely this just runs the functions one after the other just as if I ran then manually. Why does it not have the same effect?

1
Just for clarification, is the text these functions are processing in a Google Doc, a text string, a Spreadsheet cell… - JSDBroughton
If you post more code we can help you more. I just wrote a function that calls two sub-functions and everything ran sequentially as expected. Have you tried logging in your functions? Logger.log() then ctrl+enter to view the log. Are your functions doing anything asynchronously? - Patrick Neugebauer
Two things come to mind: (a) with functions executed together, the script exceeds the maximal allowed execution time (6 minutes). But then you'd get an error. (b) the second function relies on the changes made by the first function, and those change are postponed (backgrounded) by Google server. (I know this happens with spreadsheets.) In this case, a pause like Utilities.sleep(1000); between function calls may help. - user3717023
I tried pausing between the two functions with timeout, but this didn't work. I have since used a workaround. The problem with posting details is that the functions themselves are very complex. My question was posted in the hope that there was a known and common reason for this kind of failing, but as it looks like this is not the case... - DavidR

1 Answers

2
votes

Try this:

function bothTest() { 
  cleanText();
  SpreadsheetApp.flush();
  removeMultipleSpaces();
}