Привет, я использую листы Google, чтобы создать цитату, заменив текст в документе Google. Это хорошо работает в теле документа, но не в заголовке. Код, который я использую, приведен ниже.
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('Quote Menu'); // Wrap the menu name in quotes
menu.addItem('Create EN/IT Quote', 'createNewGoogleDocs');
menu.addItem('Create EN + IT Quote', 'createNewGoogleDocs');
menu.addItem('Create EN Quote', 'createNewGoogleDocs');
menu.addItem('Create IT Quote', 'createNewGoogleDocs');
menu.addToUi(); // Add parentheses to call the addToUi function
}
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('file id');
const destinationFolder = DriveApp.getFolderById('1folder id');
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Quote Data');
const rows = sheet.getDataRange().getDisplayValues();
rows.forEach(function(row, index){
if (index === 0) return;
const copy = googleDocTemplate.makeCopy(`Offer ${row[11]}. ${row[12]} - ${row[3]}` , destinationFolder)
const doc = DocumentApp.openById(copy.getId())
const body = doc.getBody();
const header = doc.getHeader();
//In these lines, we replace our replacement tokens with values from our spreadsheet row
header.replaceText('{{EN12}}', row[11])
body.replaceText('{{EN1}}', row[0]);
body.replaceText('{{EN2}}', row[1]);
body.replaceText('{{EN3}}', row[2]);
body.replaceText('{{EN4}}', row[3]);
//We make our changes permanent by saving and closing the document
doc.saveAndClose();
})
}
добавить больше деталей и больше участников





Я подумал, что ваш скрипт header.replaceText('{{EN12}}', row[11]) можно использовать в шапке. Но, судя по but not in the header, я предположил, что вы, возможно, включили заголовок первой страницы. Потому что, когда заголовок 1-й страницы включен, header.replaceText('{{EN12}}', row[11]) использовать нельзя. Если моя догадка верна, как насчет следующей модификации?
Пожалуйста, измените createNewGoogleDocs() следующим образом.
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('file id');
const destinationFolder = DriveApp.getFolderById('1folder id');
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Quote Data');
const rows = sheet.getDataRange().getDisplayValues();
rows.forEach(function (row, index) {
if (index === 0) return;
const copy = googleDocTemplate.makeCopy(`Offer ${row[11]}. ${row[12]} - ${row[3]}`, destinationFolder);
const doc = DocumentApp.openById(copy.getId());
const body = doc.getBody();
// --- I modified the below script.
const document = body.getParent();
let s = null;
while (s = document.findElement(DocumentApp.ElementType.HEADER_SECTION, s)) {
s.getElement().asHeaderSection().replaceText('{{EN12}}', row[11]);
}
// ---
body.replaceText('{{EN1}}', row[0]);
body.replaceText('{{EN2}}', row[1]);
body.replaceText('{{EN3}}', row[2]);
body.replaceText('{{EN4}}', row[3]);
doc.saveAndClose();
});
}
replaceText('{{EN12}}', row[11]) можно использовать как с заголовком первой страницы, так и с заголовками других страниц.
Это работает отлично. Я добавил еще несколько строк в 's.getElement().asHeaderSection().replaceText('VAL1', row[##])