- You want to retrieve the values from Spreadsheet and put to the template Document.
- In your Spreadsheet, all values of columns of "A" and "B" are the same for all rows like
john
and 25/05/1998
.
- You want to create one Google Document by replacing the placeholder by the values from Spreadsheet.
- The placeholder is enclosed by
%
.
- You want to achieve this using Google Apps Script.
I could understand like above. If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Flow:
The flow of this sample script is as follows.
- Retrieve values from Spreadsheet.
- Create an object for putting to Google Document.
- Copy a template Google Document.
- Put the header values to the copied Document using the object.
- In this case, the placeholder is replaced by the retrieved values.
- Put the table values using the object.
- In this case, the retrieved values are directly put to the table in the template Document.
Sample script:
Before you run the script, please set templateGoogleDocumentID
.
function myFunction() {
var templateGoogleDocumentID = "###"; // Please set the template Google Document ID.
// 1. Retrieve values from Spreadsheet.
var activeSheet = SpreadsheetApp.getActiveSheet();
var values = activeSheet.getDataRange().getValues();
// 2. Create an object for putting to Google Document.
var object = {headers: {}, table: {}};
var headerRow = values.shift();
object.headers[headerRow[0]] = values[0][0];
object.headers[headerRow[1]] = Utilities.formatDate(values[0][1], Session.getScriptTimeZone(), "yyyy/MM/dd");
object.table = values.map(r => r.splice(2, 5));
// 3. Copy a template Google Document.
var copiedTemplateDoc = DriveApp.getFileById(templateGoogleDocumentID).makeCopy();
var docId = copiedTemplateDoc.getId();
// 4. Put the header values to the copied Document using the object.
var doc = DocumentApp.openById(docId);
var body = doc.getBody();
Object.keys(object.headers).forEach(h => body.replaceText(`%${h.toLowerCase()}%`, object.headers[h]));
// 5. Put the table values using the object.
// If the table rows of Google Document are less than that of Spreadsheet, the rows are added.
var table = body.getTables()[0];
var r = object.table.length - table.getNumRows();
if (r > 0) {
for (var i = 0; i < r; i++) {
var tr = table.appendTableRow();
for (var j = 0; j < 3; j++) {
tr.appendTableCell();
}
}
}
object.table.forEach((row, i) => (row.forEach((col, j) => (table.getCell(i, j).setText(col)))));
doc.saveAndClose();
// If you want to export the Google Document as PDF file, please use the following script.
// var newFile = DriveApp.createFile(doc.getBlob());
}
Note:
- In this modified script, please enable V8 at the script editor.
References:
%
as the replacement text. But in your script,<<
and>>
are used. How about this? 2. In your script, each row creates each Document. But in your expected result, all rows are put to one Document. Unfortunately, I cannot understand about your script. How about this? 3. Can I ask you about your detail goal? – Tanaike