Google 将 Google Sheet 数据合并到 Google 文档模板的应用程序脚本未在第一行数据后迭代
Google Apps Script of merging Google Sheet data to Google Doc template is not iterating after the 1st row of data
Reference/Test Google 驱动器文件夹:
https://drive.google.com/drive/folders/1hPKQk7eRjSdlMDjiZI2BZrHIRhzVtYG5?usp=sharing
我有一个包含 1 个 Google Sheet 和 7 个 Google 文档模板的文件夹。 Google 文档模板具有占位符,用于填充来自 Google Sheet 的“PrintThis”tab/sheet 的数据。当我 运行 我的 Google Apps 脚本时,它正确地在同一驱动器文件夹中制作模板的 Google Doc 副本,其中包含第 4 行中第一行数据所需的正确值。剩余的 2 行没有迭代,并且没有为这些行创建 copy/file。当我 运行 我的代码 Exception: The document is inaccessible. Please try again later. (line 57, file "Code")
第 57 行是这一行时,我也得到一个错误: var body = DocumentApp.openById(documentId).getBody();
对于测试文件夹,我让所有 files/folder 的人都可以编辑link,所以我不知道为什么它给我一个无法访问的错误。
这是我使用的 Google 应用程序脚本代码:
function createDocument() {
var headers = Sheets.Spreadsheets.Values.get('1xSWskGS8B_3Y35I4ycAjFZQiGbfJo13O3837RKnxnpk', 'PrintThis!A3:J3');
var tactics = Sheets.Spreadsheets.Values.get('1xSWskGS8B_3Y35I4ycAjFZQiGbfJo13O3837RKnxnpk', 'PrintThis!A4:J5');
var templateR1Id = '1s6T9CEmvf6VW4X-Df0jC8SrnVflAhSyF';
var templateR3Id = '1zpeebLSo4F2Csi7K2zRyLdSchZuBhG6H';
var templateR9Id = '1edgQkTsZSDjswM577S7CEsARnd_5ohLB';
var templateCO6Id = '1sY8sY_P4NsRpXhrFqmheo4aSXtEbceTk';
var templateCO9Id = '1l78q9lUEunoS8imcWhqP7Ly91E6SYSVZ';
var templateCO12Id = '1kuCTdzQkEdtOoWq_1RgD3TYs3pBJUKFn';
var templateCO13Id = '1yuCqS_aLnphHCNIITflf15IDDZbeV2T7';
for(var i = 0; i < tactics.values.length; i++){
var patient = tactics.values[i][1];
var dueDate = tactics.values[i][2];
var bereaved = tactics.values[i][4];
var monthDue = tactics.values[i][3];
var address = tactics.values[i][5];
var city = tactics.values[i][6] ;
var state = tactics.values[i][7];
var zip = tactics.values[i][8] ;
var agency = tactics.values[i][9];
var mrnum = tactics.values[i][0];
var templatemonth;
if (monthDue == '6th Mo Due' && agency == 'CO Agency'){
templatemonth = templateCO6Id}
else if (monthDue == '9th Mo Due' && agency == 'CO Agency'){
templatemonth = templateCO9Id}
else if (monthDue == '12th Mo Due' && agency == 'CO Agency'){
templatemonth = templateCO12Id}
else if (monthDue == '13th Mo Due' && agency == 'CO Agency'){
templatemonth = templateCO13Id}
else if (monthDue == '1st Mo Due' && (agency == 'R Agency' || agency == 'R2 Agency')){
templatemonth = templateR1Id}
else if (monthDue == '3rd Mo Due' && (agency == 'R Agency' || agency == 'R2 Agency')){
templatemonth = templateR3Id}
else if (monthDue == '6th Mo Due' && (agency == 'R Agency' || agency == 'R2 Agency')){
templatemonth = templateR6Id}
else if (monthDue == '9th Mo Due' && (agency == 'R Agency' || agency == 'R2 Agency')){
templatemonth = templateR9Id}
//Make a copy of the template file
var documentId = DriveApp.getFileById(templatemonth).makeCopy().getId();
//Rename the copied file
DriveApp.getFileById(documentId).setName(mrnum + '_' + monthDue);
//Get the document body as a variable
var body = DocumentApp.openById(documentId).getBody();
//Insert the supplier name
body.replaceText('##Bereaved##', bereaved)
body.replaceText('##Address##', address)
body.replaceText('##City##', city)
body.replaceText('##State##', state)
body.replaceText('##Zip##', zip)
body.replaceText('##Letter Due Date##', dueDate)
}
}
var tactics=Sheets.Spreadsheets.Values.get('1xSWskGS8B_3Y35I4ycAjFZQiGbfJo13O3837RKnxnpk', 'PrintThis!A4:J');
我们使用 :J 而不是 J6 来包含所有可以是动态数量的行。还要确保模板采用 Google Docs 格式,而不是 Microsoft Word 或任何其他文本文件。
Reference/Test Google 驱动器文件夹:
https://drive.google.com/drive/folders/1hPKQk7eRjSdlMDjiZI2BZrHIRhzVtYG5?usp=sharing
我有一个包含 1 个 Google Sheet 和 7 个 Google 文档模板的文件夹。 Google 文档模板具有占位符,用于填充来自 Google Sheet 的“PrintThis”tab/sheet 的数据。当我 运行 我的 Google Apps 脚本时,它正确地在同一驱动器文件夹中制作模板的 Google Doc 副本,其中包含第 4 行中第一行数据所需的正确值。剩余的 2 行没有迭代,并且没有为这些行创建 copy/file。当我 运行 我的代码 Exception: The document is inaccessible. Please try again later. (line 57, file "Code")
第 57 行是这一行时,我也得到一个错误: var body = DocumentApp.openById(documentId).getBody();
对于测试文件夹,我让所有 files/folder 的人都可以编辑link,所以我不知道为什么它给我一个无法访问的错误。
这是我使用的 Google 应用程序脚本代码:
function createDocument() {
var headers = Sheets.Spreadsheets.Values.get('1xSWskGS8B_3Y35I4ycAjFZQiGbfJo13O3837RKnxnpk', 'PrintThis!A3:J3');
var tactics = Sheets.Spreadsheets.Values.get('1xSWskGS8B_3Y35I4ycAjFZQiGbfJo13O3837RKnxnpk', 'PrintThis!A4:J5');
var templateR1Id = '1s6T9CEmvf6VW4X-Df0jC8SrnVflAhSyF';
var templateR3Id = '1zpeebLSo4F2Csi7K2zRyLdSchZuBhG6H';
var templateR9Id = '1edgQkTsZSDjswM577S7CEsARnd_5ohLB';
var templateCO6Id = '1sY8sY_P4NsRpXhrFqmheo4aSXtEbceTk';
var templateCO9Id = '1l78q9lUEunoS8imcWhqP7Ly91E6SYSVZ';
var templateCO12Id = '1kuCTdzQkEdtOoWq_1RgD3TYs3pBJUKFn';
var templateCO13Id = '1yuCqS_aLnphHCNIITflf15IDDZbeV2T7';
for(var i = 0; i < tactics.values.length; i++){
var patient = tactics.values[i][1];
var dueDate = tactics.values[i][2];
var bereaved = tactics.values[i][4];
var monthDue = tactics.values[i][3];
var address = tactics.values[i][5];
var city = tactics.values[i][6] ;
var state = tactics.values[i][7];
var zip = tactics.values[i][8] ;
var agency = tactics.values[i][9];
var mrnum = tactics.values[i][0];
var templatemonth;
if (monthDue == '6th Mo Due' && agency == 'CO Agency'){
templatemonth = templateCO6Id}
else if (monthDue == '9th Mo Due' && agency == 'CO Agency'){
templatemonth = templateCO9Id}
else if (monthDue == '12th Mo Due' && agency == 'CO Agency'){
templatemonth = templateCO12Id}
else if (monthDue == '13th Mo Due' && agency == 'CO Agency'){
templatemonth = templateCO13Id}
else if (monthDue == '1st Mo Due' && (agency == 'R Agency' || agency == 'R2 Agency')){
templatemonth = templateR1Id}
else if (monthDue == '3rd Mo Due' && (agency == 'R Agency' || agency == 'R2 Agency')){
templatemonth = templateR3Id}
else if (monthDue == '6th Mo Due' && (agency == 'R Agency' || agency == 'R2 Agency')){
templatemonth = templateR6Id}
else if (monthDue == '9th Mo Due' && (agency == 'R Agency' || agency == 'R2 Agency')){
templatemonth = templateR9Id}
//Make a copy of the template file
var documentId = DriveApp.getFileById(templatemonth).makeCopy().getId();
//Rename the copied file
DriveApp.getFileById(documentId).setName(mrnum + '_' + monthDue);
//Get the document body as a variable
var body = DocumentApp.openById(documentId).getBody();
//Insert the supplier name
body.replaceText('##Bereaved##', bereaved)
body.replaceText('##Address##', address)
body.replaceText('##City##', city)
body.replaceText('##State##', state)
body.replaceText('##Zip##', zip)
body.replaceText('##Letter Due Date##', dueDate)
}
}
var tactics=Sheets.Spreadsheets.Values.get('1xSWskGS8B_3Y35I4ycAjFZQiGbfJo13O3837RKnxnpk', 'PrintThis!A4:J');
我们使用 :J 而不是 J6 来包含所有可以是动态数量的行。还要确保模板采用 Google Docs 格式,而不是 Microsoft Word 或任何其他文本文件。