【问题标题】:Google Apps Script: getCursor(); errorGoogle Apps 脚本:getCursor();错误
【发布时间】:2015-02-26 15:16:49
【问题描述】:

我在getCursor() 行收到错误We're sorry, a server error occurred. Please wait a bit a try again

这是代码。很简单。

 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.');
     }
    }

有什么想法吗?

提前致谢。

【问题讨论】:

  • 我确实尝试过相同的代码。它工作得很好。再次尝试刷新并运行该函数。
  • 您的文档是否在标题中碰巧(或曾经)有图像?如果是这样,它可能与这个问题有关:code.google.com/p/google-apps-script-issues/issues/…
  • 感谢 KRR 和 stmcallister 的 cmets。我的文档有一个标题,这似乎是根据该问题触发问题的原因。我假设 KRR 没有标题以及他/她成功运行它的原因。显然现在要删除一个标题。

标签: google-apps-script google-docs


【解决方案1】:

根据上面的 cmets,发生这种情况是因为我的文档中有一个标题。谷歌已经解决了这个问题,见https://code.google.com/p/google-apps-script-issues/issues/detail?id=4831

【讨论】:

    【解决方案2】:

    如果你不能使用光标会更好,因为它不稳定 - 谷歌在他想要的地方触摸它;-)

    如果您使用查找/替换模式 - 会更好。例子: 在文档中:

    blablabla
    blabla
    Data:
    blabla
    

    脚本:

    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) ;
    }
    

    此脚本将文本 (Data:) -> 替换为今天的日期 (06-09-2018)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 2015-05-19
      • 2017-11-17
      • 2018-06-15
      • 1970-01-01
      相关资源
      最近更新 更多