[英]Pulling data into ONE google docs file from a google sheet using google app script
Trying to pull all the data from one google sheet and automatically populate one google doc, instead of four separate files after the data pull.试图从一张谷歌表格中提取所有数据并自动填充一个谷歌文档,而不是在数据提取后填充四个单独的文件。 Missing something here which I'm scratching my head about and can't seem to find anything online.
在这里遗漏了一些我正在挠头并且似乎无法在网上找到任何东西的东西。
This is my code so far:到目前为止,这是我的代码:
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)
})
}
Appending Template Data just prior to replacing data so that all data in placed in one copy.在替换数据之前附加模板数据,以便所有数据都放在一个副本中。
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);
}
});
}
You will have to customize the below code to meet your precise needs您将必须自定义以下代码以满足您的精确需求
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";
}
Starting Data:起始数据:
A![]() |
B![]() |
C ![]() |
D![]() |
E![]() |
|
---|---|---|---|---|---|
1 ![]() |
Headline![]() |
Date![]() |
article![]() |
codr![]() |
url ![]() |
2 ![]() |
Headline1![]() |
11/1/2022 ![]() |
article1![]() |
codr1![]() |
url1![]() |
3 ![]() |
Headline2![]() |
11/2/2022 ![]() |
articel2![]() |
code2![]() |
url2![]() |
4 ![]() |
Headline3![]() |
11/3/2022 ![]() |
article2![]() |
codr2![]() |
url3![]() |
5 ![]() |
Headline4![]() |
11/4/2022 ![]() |
articel3![]() |
code3![]() |
url4![]() |
6 ![]() |
Headline5![]() |
11/5/2022 ![]() |
article3![]() |
codr3![]() |
url5![]() |
Final Document:最终文件:
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.