Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 在google文档中插入日期后,光标如何跳转到下一行?_Google Apps Script_Google Docs - Fatal编程技术网

Google apps script 在google文档中插入日期后,光标如何跳转到下一行?

Google apps script 在google文档中插入日期后,光标如何跳转到下一行?,google-apps-script,google-docs,Google Apps Script,Google Docs,我想创建一个脚本,插入今天的日期并跳到下一行。我可以插入今天的日期,但无法将光标跳到下一行。这是我试过的代码。 错误显示“参数(字符串、数字)与documentpp.Document.newPosition的方法签名不匹配”。但我认为我提供了字符串和数字作为参数。有没有办法解决这个错误 function insertAtCursor() { var cursor = DocumentApp.getActiveDocument().getCursor(); var doc = Docu

我想创建一个脚本,插入今天的日期并跳到下一行。我可以插入今天的日期,但无法将光标跳到下一行。这是我试过的代码。 错误显示“参数(字符串、数字)与documentpp.Document.newPosition的方法签名不匹配”。但我认为我提供了字符串和数字作为参数。有没有办法解决这个错误

function insertAtCursor() {  
  var cursor = DocumentApp.getActiveDocument().getCursor();
  var doc = DocumentApp.getActiveDocument();  
  
  if (cursor) {
    var date = Utilities.formatDate(new Date(), "GMT+1", "yyyy-MM-dd"); 
    var text = date.toString();
    var element = cursor.insertText(date);   
    if (element) {
      element.setBold(true);
      element.setForegroundColor("#008000")
    } else {
      DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
    }
  } else {
    DocumentApp.getUi().alert('Cannot find a cursor in the document.');
  }
  
  var position = doc.newPosition(text, text.length); 
  doc.setCursor(position);   
}
答复: 签名的方法是
元素,整数

代码修复: 更改:

var position = doc.newPosition(text, text.length); 
致:

我希望这对你有帮助

参考资料:
答案: 签名的方法是
元素,整数

代码修复: 更改:

var position = doc.newPosition(text, text.length); 
致:

我希望这对你有帮助

参考资料:

发生错误是因为
newPosition
的第一个参数应该是对象,而不是
字符串

另一方面,如果您想获取下一行,则不应使用相同的元素(在本例中为日期),而应使用与下一行对应的元素。一种方法是通过usin。在本例中,我使用
getParent
获取包含插入日期的段落,使用
getNextSibling
获取下一段落

function insertAtCursor() {  
  var cursor = DocumentApp.getActiveDocument().getCursor();
  var doc = DocumentApp.getActiveDocument();  
  
  if (cursor) {
    var date = Utilities.formatDate(new Date(), "GMT+1", "yyyy-MM-dd"); 
    var element = cursor.insertText(date);   
    if (element) {
      element.setBold(true);
      element.setForegroundColor("#008000")
    } else {
      DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
    }
  } else {
    DocumentApp.getUi().alert('Cannot find a cursor in the document.');
  }
  var par = element.getParent();
  var position = doc.newPosition(par.getNextSibling(), 0); 
  doc.setCursor(position);   
}

另一种方法是获取子编号,使用下一个子编号。一个完整的解决方案应该考虑诸如在文档结尾、表内插入的日期等情况。

旁注:


您不必使用
date.toString()
作为
实用程序。formatDate
已返回字符串。

发生错误的原因是
newPosition
的第一个参数应该是对象,而不是
字符串

另一方面,如果您想获取下一行,则不应使用相同的元素(在本例中为日期),而应使用与下一行对应的元素。一种方法是通过usin。在本例中,我使用
getParent
获取包含插入日期的段落,使用
getNextSibling
获取下一段落

function insertAtCursor() {  
  var cursor = DocumentApp.getActiveDocument().getCursor();
  var doc = DocumentApp.getActiveDocument();  
  
  if (cursor) {
    var date = Utilities.formatDate(new Date(), "GMT+1", "yyyy-MM-dd"); 
    var element = cursor.insertText(date);   
    if (element) {
      element.setBold(true);
      element.setForegroundColor("#008000")
    } else {
      DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
    }
  } else {
    DocumentApp.getUi().alert('Cannot find a cursor in the document.');
  }
  var par = element.getParent();
  var position = doc.newPosition(par.getNextSibling(), 0); 
  doc.setCursor(position);   
}

另一种方法是获取子编号,使用下一个子编号。一个完整的解决方案应该考虑诸如在文档结尾、表内插入的日期等情况。

旁注:


您不必使用
date.toString()
作为
实用程序。formatDate
已返回字符串。

您好,这解决了错误,但光标仍在日期末尾。你知道光标怎么跳到下一行的开头吗?@Marlowe是插入行末尾的日期吗?嗨,Rafa,这解决了错误,但光标仍然在日期的末尾。你知道光标怎么跳到下一行的开头吗?@Marlowe是插入到行末尾的日期吗?@RafaGuillermo为什么要删除?错误消息是关于没有使用正确的数据类型,这与JavaScript有关。请重新添加。@Rubén
参数与X的方法签名不匹配
是应用程序脚本错误,而不是JavaScript错误。这纯粹是一个气体问题。让我们来看看。已经有两个答案了。如果您需要进一步的帮助,请添加演示/测试文档结构,即告诉我们该文档是否只包含段落或包含其他元素类型,如列表项、表格等,以及日期是否总是添加在最后一个元素之前。@RafaGuillermo为什么要删除?错误消息是关于没有使用正确的数据类型,这与JavaScript有关。请重新添加。@Rubén
参数与X的方法签名不匹配
是应用程序脚本错误,而不是JavaScript错误。这纯粹是一个气体问题。让我们来看看。已经有两个答案了。如果您需要进一步的帮助,请添加演示/测试文档结构,即告诉我们该文档是否仅包含段落或包含其他元素类型,如列表项、表格等,以及日期是否始终添加在最后一个元素之前。