Google apps script 如何让代码在光标处插入,而不是在文档末尾自动追加

Google apps script 如何让代码在光标处插入,而不是在文档末尾自动追加,google-apps-script,Google Apps Script,我正在尝试构建一个模板生成器Google Docs插件(我已经成功实现了),它可以将1个文档中的内容拉入当前文档,但我的问题是,它的设置方式会将拉入的每个元素都附加到模板生成器Google Docs插件中,每次进入文档末尾,而不是光标位置,我的所有尝试都失败了 以下是插入模板部件的函数: function insert() { var baseDoc = DocumentApp.getActiveDocument(); var body = baseDoc.getBody

我正在尝试构建一个模板生成器Google Docs插件(我已经成功实现了),它可以将1个文档中的内容拉入当前文档,但我的问题是,它的设置方式会将拉入的每个元素都附加到模板生成器Google Docs插件中,每次进入文档末尾,而不是光标位置,我的所有尝试都失败了

以下是插入模板部件的函数:

function insert() {

      var baseDoc = DocumentApp.getActiveDocument();

    var body = baseDoc.getBody();

    var otherBody = DocumentApp.openById(docID).getBody();

    var totalElements = otherBody.getNumChildren();

    for (var j = 0; j < totalElements; ++j) {
        var element = otherBody.getChild(j).copy();
        var type = element.getType();
        if (type == DocumentApp.ElementType.PARAGRAPH)
            body.appendParagraph(element);
        else if (type == DocumentApp.ElementType.TABLE)
            body.appendTable(element);
        else if (type == DocumentApp.ElementType.LIST_ITEM)
            body.appendListItem(element);
        else if (type == DocumentApp.ElementType.INLINE_IMAGE)
            body.appendImage(element);

        // add other element types as you want

        else
            throw new Error("According to the doc this type couldn't appear in the body: " + type);
    }
}
函数插入(){
var baseDoc=DocumentApp.getActiveDocument();
var body=baseDoc.getBody();
var otherBody=DocumentApp.openById(docID.getBody();
var totalElements=otherBody.getNumChildren();
对于(var j=0;j
  • 您希望将
    otherBody
    中的段落、表格、列表和图像插入到活动文档的光标位置
如果我的理解是正确的,那么这个答案呢?请把这看作是几个可能的答案之一

修改点:
  • 在您的情况下,首先,请检索活动文档的光标位置
  • appendparation
    将段落追加到文档的最后一部分。因此,在这种情况下,请使用
    插入段落
    • 在这种情况下,请设置位置
当上述各点反映到脚本中时,它将变成如下所示

修改脚本: 请将光标放在活动文档上,然后运行函数
insert
。通过这种方式,
otherBody
中的段落、表格、列表和图像被插入到光标位置

function insert() {
  var docID = "###";  // Please set the Document ID.

  var baseDoc = DocumentApp.getActiveDocument();
  var body = baseDoc.getBody();
  var otherBody = DocumentApp.openById(docID).getBody();

  var cursor = baseDoc.getCursor();  // Added
  var cursorPos = baseDoc.getBody().getChildIndex(cursor.getElement());  // Added

  var totalElements = otherBody.getNumChildren();
  for (var j = 0; j < totalElements; ++j) {
    var element = otherBody.getChild(j).copy();
    var type = element.getType();
    if (type == DocumentApp.ElementType.PARAGRAPH)
      body.insertParagraph(cursorPos + j, element);  // Modified
    else if (type == DocumentApp.ElementType.TABLE)
      body.insertTable(cursorPos + j, element);  // Modified
    else if (type == DocumentApp.ElementType.LIST_ITEM)
      body.insertListItem(cursorPos + j, element);  // Modified
    else if (type == DocumentApp.ElementType.INLINE_IMAGE)
      body.insertImage(cursorPos + j, element);  // Modified
    else
      throw new Error("According to the doc this type couldn't appear in the body: " + type);
  }
}
函数插入(){
var docID=“####”;//请设置文档ID。
var baseDoc=DocumentApp.getActiveDocument();
var body=baseDoc.getBody();
var otherBody=DocumentApp.openById(docID.getBody();
var cursor=baseDoc.getCursor();//已添加
var cursorPos=baseDoc.getBody().getChildIndex(cursor.getElement());//已添加
var totalElements=otherBody.getNumChildren();
对于(var j=0;j
参考资料:

如果我误解了你的问题,而这不是你想要的结果,我道歉。

@Tasmto感谢你的回答。我很高兴你的问题解决了。