/**
*
* @param {GoogleAppsScript.Document.Document} doc
* @param {Record<string,unknown>} data
*/
function fillDoc_(doc, data) {
const body = doc.getBody();
Object.entries(data).forEach(([key, value]) => {
body.replaceText(`(?i){{${key}}}`, value);
});
body.replaceText(`(?i){{.*?}}`, '');
}
const file = DriveApp.getFileById('192avJeIyh524ofvFvuQRaf9QPyqH7FKBEzwNLoHJa-g').makeCopy();
const doc = DocumentApp.openById(file.getId());
Важно, заменяемый текст в шаблоне должен быть обернут в {{заменяемый текст}}
.
const values = SpreadsheetApp.getActive()
.getRange("'Заполнение Документа данными из Таблицы'!2:2")
.getDisplayValues()[0];
const data = {
заголовок: 'тестовый заголовок',
имя: values[1],
};
fillDoc_(doc, data);
body.replaceText("{Фамилия Имя Отчество}", "");
body.replaceText("{Космодемьянская д. 13 кв. 29}", "");
body.replaceText("{1111 111111}", "");
body.replaceText("{почта}", "");
body.replaceText("{01.01.2000}", "");
body.replaceText("{Фамилия Имя Отчество}", "");
body.replaceText("{Космодемьянская д. 13 кв. 29}", "");
body.replaceText("{2222 333333}", "");
body.replaceText("{мясо}", "");
body.replaceText("{01.01.2222}", "");
body.replaceText("{ДР33.33.3333}", "");
body.replaceText("{сок}", "");
body.replaceText("{хлеб}", "");
body.replaceText("{номер телефона}", "");
body.replaceText("{адрес}", "");
body.replaceText("{1 телефон}", "");
body.replaceText("{2.Фамилия Имя Отчество}", "");
body.replaceText("{2 телефон}", "");
body.replaceText('{.*?}', '');
=IMPORTDATA("https://script.google.com/macros/s/AKfycbzMBdr9yLrFPTOiRdEAr-2tHp4vkTasa6t8ME9_yxFDwCqQqg-JZEOQDHTvfrde7ahM/exec?fileId=" & C3)
C3
- ID ДокументаGET https://www.googleapis.com/drive/v3/files/fileId
возвращает список возможных экспортов{
"exportLinks": {
"application/rtf": "https://docs.google.com/feeds/download/documents/export/Export?id=ABC&exportFormat=rtf",
"application/vnd.oasis.opendocument.text": "https://docs.google.com/feeds/download/documents/export/Export?id=ABC&exportFormat=odt",
"text/html": "https://docs.google.com/feeds/download/documents/export/Export?id=ABC&exportFormat=html",
"application/pdf": "https://docs.google.com/feeds/download/documents/export/Export?id=ABC&exportFormat=pdf",
"application/epub+zip": "https://docs.google.com/feeds/download/documents/export/Export?id=ABC&exportFormat=epub",
"application/zip": "https://docs.google.com/feeds/download/documents/export/Export?id=ABC&exportFormat=zip",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "https://docs.google.com/feeds/download/documents/export/Export?id=ABC&exportFormat=docx",
"text/plain": "https://docs.google.com/feeds/download/documents/export/Export?id=ABC&exportFormat=txt"
}
}
https://www.googleapis.com/auth/drive.readonly
, и вызовитеGET https://docs.google.com/feeds/download/documents/export/Export?id=ABC&exportFormat=txt
“”«»
/* eslint-disable require-jsdoc */
/* eslint-disable no-undef */
function doAction() {
var result = readBaseData('22-05-2019');
// If found data for this date...
if (result != null) {
var doc = createNewDoc(' iskovoe ' + result[0][0]);
fillTemplate(doc, result);
}
}
// inDoc - its a empty template, inData - data for filling template
function fillTemplate(inDoc, inData) {
// Getting count of files in folder
var NUM = DocsList.getFolderById(
'1a3uCa9wRyOzio-C7FVacOykuLoxCqTs3'
).getFiles().length;
// Replace masks (Don't work [NUM] or $NUM or something else)
inDoc.replaceText(
'x',
'https://docs.google.com/spreadsheets/d//edit#gid=0...'
);
inDoc.replaceText(
'x',
'https://docs.google.com/spreadsheets/d//edit#gid=0...'
);
inDoc.replaceText(
'x',
'https://docs.google.com/spreadsheets/d//edit#gid=0...'
);
inDoc.replaceText(
'x',
'https://docs.google.com/spreadsheets/d//edit#gid=0...'
);
// get first table in document
var table = curDoc.getTables()[0];
for (i = 0; i < inData.length; i++) {
var row = table.appendTableRow();
row.appendTableCell(inData[i][1]);
row.appendTableCell(inData[i][2]);
}
curDoc.saveAndClose();
}
function createNewDoc() {
// Making copy of blank file
var blankDoc = DocsList.getFileById(
'19vYiThaKdgQ6eeiRg5nPewzP_Eab7nkLUwyt49jCC1Q'
).makeCopy(iskovoe);
blankDoc.addToFolder(
DocsList.getFolderById('1a3uCa9wRyOzio-C7FVacOykuLoxCqTs3')
);
// return working doc and table base
return (curDoc = DocumentApp.openById(blankDoc.getId()));
}
// get range and sort only for day from parameter
function readBaseData(inDate) {
var ssDoc = SpreadsheetApp.openById(
'1ZCNO6Iae2RwDdKAlQX6TF5mpp4xHHB4v1mXK233uJls'
).getActiveSheet();
// get filled range
var data = ssDoc.getDataRange().getValues();
// create new array with data to inDate,
// for start from 1 because first row is text column headers
var filteredData = new Array([]);
for (i = 1; i < data.length; i++) {
data[i][0] = convertDate(data[i][0]);
if (data[i][0] == inDate && data[i][3] == 1) {
filteredData[i - 1] = data[i];
}
}
return filteredData;
}
https://drive.google.com/drive/search?q=type:document%20%D0%B4%D0%BB%D1%8F
embedded=true
<iframe src="https://docs.google.com/viewerng/viewer?url=https://cit.tsn.47edu.ru/doc/Programma_provedenia_regionalnykh_UTS_24_11_2018.docx&embedded=true">
</iframe>
<iframe src="https://view.officeapps.live.com/op/embed.aspx?src=https://cit.tsn.47edu.ru/doc/Programma_provedenia_regionalnykh_UTS_24_11_2018.docx">
</iframe>