[英]Pulling data into ONE google docs file from a google sheet using google app script
試圖從一張谷歌表格中提取所有數據並自動填充一個谷歌文檔,而不是在數據提取后填充四個單獨的文件。 在這里遺漏了一些我正在撓頭並且似乎無法在網上找到任何東西的東西。
到目前為止,這是我的代碼:
function createNewGoogleDocs() {
//This value should be the id of your document
const googleDocTemplate = DriveApp.getFileById('1kVXtatdcdlKRYzDADnIckSYcg8N3SIixn-6lEHsRMbk');
//This value should be the id of the folder where you want your completed documents stored
const destinationFolder = DriveApp.getFolderById('1ZmfdojPXdBkW93EECH9rd9Vt06Cqx7tI')
//Here we store the sheet as a variable
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Data')
//Now we get all of the values as a 2D array. Merging sheet data into neccessary value type to work with.
const rows = sheet.getDataRange().getValues();
//Start processing each spreadsheet row (each array)
rows.forEach(function(row, index){
//Here we check if this row is the headers, if so we skip it
if (index === 0) return;
//Here we check if a document has already been generated by looking at 'Document Link', if so we skip it
if (row[5]) return;
//Using the row data in a template literal, we make a copy of our template document in our destinationFolder
const copy = googleDocTemplate.makeCopy(`${row[1]}, ${row[0]} Article Details` , destinationFolder)
//Once we have the copy, we then open it using the DocumentApp
const doc = DocumentApp.openById(copy.getId())
//All of the content lives in the body, so we get that for editing
const body = doc.getBody();
//In this line we do some friendly date formatting
const friendlyDate = new Date(row[1]).toLocaleDateString();
//In these lines, we replace our replacement tokens with values from our spreadsheet row
body.replaceText('{{Headline}}', row[0]);
body.replaceText('{{Timestamp}}', friendlyDate);
body.replaceText('{{Article}}', row[2]);
body.replaceText('{{CODR}}', row[3]);
body.replaceText('{{URL}}', row[4]);
//We make our changes permanent by saving and closing the document
doc.saveAndClose();
//Store the url of our new document in a variable
const url = doc.getUrl();
//Write that value back to the 'Document Link' column in the spreadsheet.
sheet.getRange(index + 1, 6).setValue(url)
})
}
在替換數據之前附加模板數據,以便所有數據都放在一個副本中。
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('1OuxpEp8bzD7ndFZ9SUtfqmWeat-4L5SDvjcZW5hsJ7g');
const destinationFolder = DriveApp.getFolderById(gobj.globals.folder1id);
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0')
const [h, ...vs] = sh.getDataRange().getValues();
const date = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy/MM/dd")
const copy = googleDocTemplate.makeCopy(`${date} Article Details`, destinationFolder)
vs.forEach((r, i) => {
if (!r[5]) {
appendToTemplate(i + 1, copy.getId())
const doc = DocumentApp.openById(copy.getId());
const url = doc.getUrl();
const body = doc.getBody();
body.replaceText(`{{Headline${i + 1}}}`, r[0]);
body.replaceText(`{{TimeStamp${i + 1}}}`, new Date(r[1]).toLocaleDateString());
body.replaceText(`{{Article${i + 1}}}`, r[2]);
body.replaceText(`{{CODR${i + 1}}}`, r[3]);
body.replaceText(`{{URL${i + 1}}}`, r[4]);
doc.saveAndClose();
sh.getRange(i + 2, 6).setValue(url);
}
});
}
您將必須自定義以下代碼以滿足您的精確需求
function appendToTemplate(n, id = "1OuxpEp8bzD7ndFZ9SUtfqmWeat-4L5SDvjcZW5hsJ7g") {
const d = DocumentApp.openById(id);
const b = d.getBody();
const s = `Line 1 is {{Headline${n}}}\nLine 2 is {{TimeStamp${n}}}\nLine 3 is {{Article${n}}}\nLine 4 is {{CODR${n}}}\nLine 5 is {{URL${n}}}`;
d.appendParagraph(s);
d.saveAndClose();
const end = "is near";
}
起始數據:
一種 | 乙 | C | 丁 | 乙 | |
---|---|---|---|---|---|
1個 | 標題 | 日期 | 文章 | 編碼器 | url |
2個 | 標題1 | 11/1/2022 | 第1條 | 編碼器1 | 網址 1 |
3個 | 標題2 | 11/2/2022 | 第2條 | 代碼2 | 網址2 |
4個 | 標題3 | 11/3/2022 | 第2條 | 編碼器2 | 網址3 |
5個 | 標題4 | 11/4/2022 | 第 3 條 | 代碼3 | 網址4 |
6個 | 標題5 | 11/5/2022 | 第三條 | 編碼器3 | 網址5 |
最終文件:
Line 1 is Headline1
Line 2 is 11/1/2022
Line 3 is article1
Line 4 is codr1
Line 5 is url1
Line 1 is Headline2
Line 2 is 11/2/2022
Line 3 is articel2
Line 4 is code2
Line 5 is url2
Line 1 is Headline3
Line 2 is 11/3/2022
Line 3 is article2
Line 4 is codr2
Line 5 is url3
Line 1 is Headline4
Line 2 is 11/4/2022
Line 3 is articel3
Line 4 is code3
Line 5 is url4
Line 1 is Headline5
Line 2 is 11/5/2022
Line 3 is article3
Line 4 is codr3
Line 5 is url5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.