[英]Google Appscript copy and paste google docs content without space in the header space
我已經按照這個腳本如何在 Google 文檔之間復制內容和格式? 從一個谷歌文檔復制內容並將其粘貼到另一個,效果很好,但是每次粘貼內容時,粘貼內容的頂部都有一個空格,見下文。 如何正確粘貼內容?
源文件: https://docs.google.com/document/d/1xVpJM4hSN3fosFXR16JbZ1_7r0_PxV92T-G24X5LQRo/edit
目標文件: https://docs.google.com/document/d/1g9oon4e0FDBF2fbexVCR-uxKko3B6-Hpj850kiH3qXo/edit
基本上源文件中的表格將多次復制並粘貼到目標文件,表格應該並排放置在目標文件上,頂部沒有空格,這會破壞格式。
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();
}
為什么不將表格單元格復制到右側的新單元格中。 嘗試這個。
首先,我將源代碼或模板復制到一個新文件中。 然后我找到表格並遍歷每一行,將單元格從第 1 列復制到新的第 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.