0

enter image description here

I have tried innerHTML instead of getBody() but still get an error, and any help with debugging on apps-script, mine does not seem to work.

 function findText(findme,colour,desc,rule_name) {
  var body = DocumentApp.getActiveDocument().getBody();
  var regExp = case_insensitive(findme);
  var foundElement = body.findText(regExp);
  while (foundElement != null) {
    var foundText = foundElement.getElement().asText();
    var start = foundElement.getStartOffset();
    var end = foundElement.getEndOffsetInclusive();
    foundText.setBackgroundColor(start, end, colour);
    number_oresults++;
    foundElement = body.findText(regExp, foundElement);
    var pusher = '<p><span style="background-color:'+colour+'"><b>'+rule_name+'</b> - '+ desc +'</span></p>';
    results.push(pusher);
   }
} 
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    See [mcve]. *DO NOT use images of code. Copy the actual text from your code editor, paste it into the question, then format it as code. This helps others more easily read and test your code* – TheMaster Sep 10 '20 at 21:27
  • 2
    Please read [ask], especially the section titled "Write a title that summarizes the specific problem" The current title summarizes 90% of the questions about Google Apps Script on the site :). – Heretic Monkey Sep 10 '20 at 21:38

2 Answers2

1

Looks like you're running a standalone script, but DocumentApp.getActiveDocument() is only available in container-bound scripts.

You can copy-paste your script into a new script bound to that Google Doc or use one of the other open methods:

Diego
  • 9,261
  • 2
  • 19
  • 33
  • `DocumentApp.getActiveDocument()` is also available for standalone scripts ... see [my answer](https://stackoverflow.com/a/63838056/1595451) for details – Rubén Sep 10 '20 at 21:43
  • Thanks to everyone for answering my questions, although I have run into a new problem, by using an attached script the script will only run within itself and not on the docs' side bar. Any help? – afrologicinsect Sep 17 '20 at 09:16
1

Sometimes the active methods when used in method chainging can throw errors like the one shown in your screenshot (`can't read property something of null) See Why Class Range getValues sometimes returns [[]] when chained to Class Sheet getActiveRange?

Try replacing

var body = DocumentApp.getActiveDocument().getBody();

by

var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();

It's worthy to note that DocumentApp.getActiveDocument() can only be used on bounded scripts and on standalone scripts when be executed as add-on either by using Run > Test as add-on... or by publishing the script as a G Suite Editor add-on for a Google Documents and executing the script from the UI.

NOTE: The following works fine on a standalone script executed Run > Test as add-on...

function onOpen(e) {
  DocumentApp.getUi()
  .createAddonMenu()
  .addItem('Test 1', 'doSomething1')
  .addItem('Test 2', 'doSomething2')
  .addToUi()
}

function doSomething1(){
  var body = DocumentApp.getActiveDocument().getBody();
  body.appendParagraph('Test 1');
}

function doSomething2(){
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  body.appendParagraph('Test 2');
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Interesting behavior. Thanks for pointing it out to me. – Diego Sep 10 '20 at 22:11
  • @Diego I just did a test. `DocumentApp.getActiveDocument().getBody();` worked as expected on a standalone project usign the default runtime. Anyway I think that is better to avoid to use method chaining with "active" methods. – Rubén Sep 10 '20 at 22:36
  • Hi Ruben, so the script I am running was clasped as a standalone project, when I create a bound script and use `getById( )` it works, but only runs from the script editor and not the UI/sidebar itself. any help? – afrologicinsect Sep 19 '20 at 17:13
  • `openById( )` sorry. – afrologicinsect Sep 19 '20 at 17:33
  • @afrologicinsect I'm sorry but it's not clear to me what do yo mean by " and not the UI/sidedar" (I would like to see the code) Please post a new question. – Rubén Sep 19 '20 at 18:01
  • Okay thanks for the reply, I just posted the question here: https://stackoverflow.com/questions/63971833/how-can-i-make-this-work-in-google-apps-script-run-from-the-client-side – afrologicinsect Sep 19 '20 at 18:04