简体   繁体   English

Google Apps脚本:getCursor(); 错误

[英]Google Apps Script: getCursor(); error

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

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. 提前致谢。

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 Google已解决此问题,请参阅https://code.google.com/p/google-apps-script-issues/issues/detail?id=4831

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. 如果你使用find / replace架构 - 那就更好了。 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) 此脚本将文本(数据:) - >替换为今天的日期(06-09-2018)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM