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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.