![](/img/trans.png)
[英]Looking for a way to add borders to a paragraph in a google doc using a Google Apps Script or perhaps the DOCS API?
[英]Copying a section from Google Docs to another Doc using Apps Script
我已經成功地使用此代碼將一個文檔的全部內容復制到另一個文檔中:
const newestFile = DocumentApp.openById("ID").getBody();
const archive = DocumentApp.openById("ID").getBody();
let index = 12;
let el, type;
for (let i = 0; i < newestFile.getNumChildren(); i++){
el = newestFile.getChild(i);
type = el.getType();
switch (type){
case DocumentApp.ElementType.PARAGRAPH:
archive.insertParagraph(index,el.copy());
index++;
break;
case DocumentApp.ElementType.LIST_ITEM:
archive.insertListItem(index,el.copy());
index++;
break;
case DocumentApp.ElementType.TABLE:
archive.insertTable(index,el.copy());
index++;
break;
}
}
但是,我現在需要將一個文檔的一部分復制到另一個文檔中,但我無法弄清楚。 如果我知道如何獲取任何元素的 body 索引,我可以用同樣的方法來做,但我不知道這是否可行。 我需要復制的文本將始終以特定文本(“當前周”)開頭,並在特定文本(“ARCHIVE”)之前立即結束。
描述
這是一個如何在特定文本之間復制的簡單示例。 我只介紹了段落和表格,但可以處理任何其他類型的元素。
測試文件
腳本
function myFunction() {
try {
let doc = DocumentApp.getActiveDocument();
let body = doc.getBody();
let count = body.getNumChildren();
doc = DocumentApp.create("dummy");
let copy = doc.getBody();
let start = false;
for( let i=0; i<count; i++ ) {
let child = body.getChild(i);
if( child.getType() == DocumentApp.ElementType.PARAGRAPH ) {
if( child.asParagraph().findText("Current Week") ) start = true;
if( start ) copy.appendParagraph(child.asParagraph().copy());
if( child.asParagraph().findText("ARCHIVE") ) break;
}
else if( child.getType() == DocumentApp.ElementType.TABLE ) {
if( start ) copy.appendTable(child.asTable().copy());
}
}
}
catch(err) {
console.log("Error in myFunction - "+err)
}
}
參考
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.