简体   繁体   English

header 空格内Google Appscript 复制粘贴google docs 内容无空格

[英]Google Appscript copy and paste google docs content without space in the header space

I have followed this script How to copy content and formatting between Google Docs?我已经按照这个脚本如何在 Google 文档之间复制内容和格式? to copy the content from one google doc and paste it to another, which works great, however everytime the content is pasted, there is a space on top of the pasted content, see below.从一个谷歌文档复制内容并将其粘贴到另一个,效果很好,但是每次粘贴内容时,粘贴内容的顶部都有一个空格,见下文。 How can the content be pasted properly?如何正确粘贴内容?

在此处输入图像描述

Source file: https://docs.google.com/document/d/1xVpJM4hSN3fosFXR16JbZ1_7r0_PxV92T-G24X5LQRo/edit源文件: https://docs.google.com/document/d/1xVpJM4hSN3fosFXR16JbZ1_7r0_PxV92T-G24X5LQRo/edit
Target file: https://docs.google.com/document/d/1g9oon4e0FDBF2fbexVCR-uxKko3B6-Hpj850kiH3qXo/edit目标文件: https://docs.google.com/document/d/1g9oon4e0FDBF2fbexVCR-uxKko3B6-Hpj850kiH3qXo/edit

Basically the table from the source file will get copied and pasted to the target file for multiple times, and the tables should sit side by side on the target file without space on top which breaks the format.基本上源文件中的表格将多次复制并粘贴到目标文件,表格应该并排放置在目标文件上,顶部没有空格,这会破坏格式。

appscript is inbedded in the source file appscript 嵌入在源文件中

function copyDoc() {
  var sourceDoc = DocumentApp.getActiveDocument().getBody();
//  var targetDoc = DocumentApp.create('CopyOf'+DocumentApp.getActiveDocument().getName());
  var targetDoc = DocumentApp.openById('1g9oon4e0FDBF2fbexVCR-uxKko3B6-Hpj850kiH3qXo');
  var totalElements = sourceDoc.getNumChildren();

  for( var j = 0; j < totalElements; ++j ) {
    var body = targetDoc.getBody()
    var element = sourceDoc.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);
      }
//    ...add other conditions (headers, footers...
    }
  targetDoc.saveAndClose();
}

Why not copy the table cell into a new cell to the right.为什么不将表格单元格复制到右侧的新单元格中。 Try this.尝试这个。

First I copy the Source or template to a new file.首先,我将源代码或模板复制到一个新文件中。 Then I find the table and loop through each row duplicating the cell from column 1 to new column 2. If you wanted to add another row you could duplicate the first row as many times as you want to the new table.然后我找到表格并遍历每一行,将单元格从第 1 列复制到新的第 2 列。如果你想添加另一行,你可以将第一行复制到新表格中任意多次。

However I notice you are using replacable text.但是我注意到您使用的是可替换文本。 You will have to have a different script to replace the text in column 1 vs. column 2.您必须使用不同的脚本来替换第 1 列和第 2 列中的文本。

function copyDoc() {
  try {
    let source = DocumentApp.getActiveDocument();
    let file = DriveApp.getFileById(source.getId()).makeCopy("Target");
    let target = DocumentApp.openById(file.getId());
    let body = target.getBody();
    for( let i=0; i<body.getNumChildren(); i++ ) {
      let child = body.getChild(i);
      if( child.getType() === DocumentApp.ElementType.TABLE ) {
        child = child.asTable();
        for( let i=0; i<child.getNumRows(); i++ ) {
          let sRow = child.getRow(i)
          let tRow = sRow.copy();
          for( let j=0; j<tRow.getNumCells(); j++ ) {
            let cell = tRow.getCell(j).copy();
            sRow.appendTableCell(cell);
          }
        }
      }
    }
  }
  catch(err){
    console.log(err);
  }
}

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

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