0
votes

I'm getting the error We're sorry, a server error occurred. Please wait a bit a try again on the getCursor() line.

Here's the code. It's very simple.

 function onOpen() {
      DocumentApp.getUi() // Or DocumentApp or FormApp.
          .createMenu('Checkbox')
          .addItem('Insert Checkbox', 'insertCheckbox')
          .addToUi();
    }

    function insertCheckbox() {         
     var cursor = DocumentApp.getActiveDocument().getCursor(); // CODE BREAKS HERE


     if (cursor) {
       // Attempt to insert text at the cursor position. If the insertion returns null, the cursor's
       // containing element doesn't allow insertions, so show the user an error message.
       var element = cursor.insertText('ಠ‿ಠ');
       if (element) {
         element.setBold(true);
       } else {
         DocumentApp.getUi().alert('Cannot insert text here.');
       }
     } else {
       DocumentApp.getUi().alert('Cannot find a cursor.');
     }
    }

Any thoughts?

Thanks in advance.

2
I did tried the same code. It is working perfectly fine.Try refreshing again and run the function. - KRR
Does your doc happen to have (or ever had) an image in the Header? If so, it could be related to this issue: code.google.com/p/google-apps-script-issues/issues/… - stmcallister
Thank you KRR and stmcallister for your comments. My doc has a header and that seems to be what's triggering the problem according to that issue. I assume KRR does not have a header and why he/she run it successfully. Apparently there is now to delete a header. - yeremy

2 Answers

0
votes

Per the comments above, this was happening because I have a Header in my Document. Google has fixed the issue, see https://code.google.com/p/google-apps-script-issues/issues/detail?id=4831

0
votes

Better if you can't use Cursor, because it not stable - google touch it where he want ;-)

If you use find/replace schema - it's better. Example: in Docs:

blablabla
blabla
Data:
blabla

Script:

function replaceDate(){
 var body = DocumentApp.getActiveDocument().getBody();
 var d = new Date();
  var pattern = 'Date:';
  var yesterdayDate = Utilities.formatDate(new Date(d.getTime()-1*(24*3600*1000)), "GMT", "dd/MM/yyy"); 
 var todayDate = Utilities.formatDate(d, "GMT", "dd-MM-yyy"); 
 body.replaceText(pattern,todayDate) ;
}

This script replace text (Data:) -> to today date (06-09-2018)