1

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?

5
  • Just for clarification, is the text these functions are processing in a Google Doc, a text string, a Spreadsheet cell… Commented Jun 18, 2016 at 20:40
  • 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? Commented Jun 19, 2016 at 2:03
  • 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. Commented Jun 19, 2016 at 4:03
  • 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... Commented Jun 19, 2016 at 12:55
  • You may want to take a look at this. developers.google.com/apps-script/reference/spreadsheet/… Commented Jun 21, 2016 at 14:16

1 Answer 1

2

Try this:

function bothTest() { 
  cleanText();
  SpreadsheetApp.flush();
  removeMultipleSpaces();
}
Sign up to request clarification or add additional context in comments.

3 Comments

As an aside, any kind of parsing or processing of text is almost always more efficient as a Regular Expression.
true, but I'm using Docs and regex is limited in some annoying ways (for example, cannot replace partial selected text!)
My bad: there's no corresponding function for gDocs. how are you accessing the document, by key or getActiveDocument()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.