这段代码应该将我的电子表格中的数据转换为文档,然后再转换为pdf,但它只做最后一行



我有一个脚本,它应该从谷歌工作表中导出数据,将其输入文档并存储在适当的文件夹中,然后将新文档转换为PDF,然后将其存储在自己的文件夹中。

当我独立运行这两个函数时,它们会一直执行,直到创建完所有文档。

将它们放在一起,如下所示,它们只对最后一行数据进行处理,而不对其他13…进行处理

有什么想法为什么它不循环吗?

function createDoc() {
console.log("first");
// To pull the headers of each column - insert spreadsheet URL and header array
var headers = Sheets.Spreadsheets.Values.get('1846CxmPdoc2VBW6GxaybCPW1_u2swO1jooIBiF2Yl90', 'A2:AA2');
// To pull the variables for each header - insert spreadsheet URL and variable array
var variables = Sheets.Spreadsheets.Values.get('1846CxmPdoc2VBW6GxaybCPW1_u2swO1jooIBiF2Yl90', 'A3:AA14');
// To pull the template document for the header and variables to be input by the program
var templateId = '1ONhT3n4Pr49BL6xEM_ykO9UVi8xZriA2fVAZjoFi2qI';

//Loop to run through all variables in spreadsheet
for(var i = 0; i < variables.values.length; i++) {

//Create the variables which will be used to transfer the data from the spreadsheet to the document. The first cell in the row is 0 and then from left to right, increases in its number. Only change variable name and the number.
var client = variables.values[i][0];
var agent = variables.values[i][1];
var aaddress = variables.values[i][2];
var acity = variables.values[i][3];
var caddress = variables.values[i][4];
var ccity = variables.values[i][5];
var suopen = variables.values[i][6];
var suclose = variables.values[i][7];
var moopen = variables.values[i][8];
var moclose = variables.values[i][9];
var tuopen = variables.values[i][10];
var tuclose = variables.values[i][11];
var weopen = variables.values[i][12];
var weclose = variables.values[i][13];
var thopen = variables.values[i][14];
var thclose = variables.values[i][15];
var fropen = variables.values[i][16];
var frclose = variables.values[i][17];
var saopen = variables.values[i][18];
var saclose = variables.values[i][19];
var price = variables.values[i][20];
var appayment = variables.values[i][21];
var mpayment = variables.values[i][22];
var junepayment = variables.values[i][23];
var julypayment = variables.values[i][24];
var aupayment = variables.values[i][25];
var sepayment = variables.values[i][26];
}
// Make a copy of the template file
const documentId = DriveApp.getFileById('1ONhT3n4Pr49BL6xEM_ykO9UVi8xZriA2fVAZjoFi2qI').makeCopy().getId();
// Rename the copied file
DriveApp.getFileById(documentId).setName('2022' + " " + client + " " + 'Pool Management Proposal');

// Set copied file's destination variable;
var folderId = DriveApp.getFolderById('1YVhLzwZ9CI5-iTR1SKHF5ykNVqZQvQY9');

// Send copied file to this folder
var fileId = DriveApp.getFileById(documentId);
fileId.moveTo(folderId); 
// Get the document body as a variable
const  OpenDoc = DocumentApp.openById(documentId);
const body = OpenDoc.getBody();
// Insert Data from sheet to document. ##info## is the placeholder text in the document, and the second variable is the variable we set above to codify the column's data in the sheet.
body.replaceText('##Agent Name##', agent);
body.replaceText('##Agent Address##', aaddress);
body.replaceText('##Agent City/Zip##', acity);
body.replaceText('##Client Name##', client)
body.replaceText('##Client Address##', caddress);
body.replaceText('##Client City/Zip##', ccity);
body.replaceText('##Contract Price##', price);
body.replaceText('##April Payment##', appayment);
body.replaceText('##May Payment##', mpayment);
body.replaceText('##June Payment##', junepayment);
body.replaceText('##July Payment##', julypayment);
body.replaceText('##August Payment##', aupayment);
body.replaceText('##September Payment##', sepayment);
body.replaceText('##Sunday Open##', suopen);
body.replaceText('##Sunday Close##', suclose);
body.replaceText('##Monday Open##', moopen);
body.replaceText('##Monday Close##', moclose);
body.replaceText('##Tuesday Open##', tuopen);
body.replaceText('##Tuesday Close##', tuclose);
body.replaceText('##Wednesday Open##', weopen);
body.replaceText('##Wednesday Close##', weclose);
body.replaceText('##Thursday Open##', thopen);
body.replaceText('##Thursday Close##', thclose);
body.replaceText('##Friday Open##', fropen);
body.replaceText('##Friday Close##', frclose);
body.replaceText('##Saturday Open##', saopen);
body.replaceText('##Saturday Close##', saclose);
OpenDoc.saveAndClose();
var pdfFolder = DriveApp.getFolderById("1_idXGdZo0l_U1IxuaLDUqrk0HjdfZvsg"); 
var templateFile = DriveApp.getFileById(documentId);
var theBlob = templateFile.getBlob().getAs('application/pdf');
var newPDFFile = folderId.createFile(theBlob);
newPDFFile.moveTo(pdfFolder);
var fileName = templateFile.getName().replace(".", ""); 
// otherwise filename will be shortened after full stop   
newPDFFile.setName(fileName + ".pdf").getId();
}
function myfunk() {
const vs = Sheets.Spreadsheets.Values.get("ssid",'Sheet0!A1:J21');
SpreadsheetApp.getActive().getSheetByName('Sheet1').getRange(1,1,vs.values.length,vs.values[0].length).setValues(vs.values);
}

我想了想你在说什么,你是对的,它的行为似乎不像和数组,所以我尝试了这个:

function myfunk() {
const ss =  SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
let vs = Array.from(Sheets.Spreadsheets.Values.get("ssid",'Sheet0!A1:J21').values);
let va = vs.filter((r,i) => i % 2 == 0);
SpreadsheetApp.getActive().getSheetByName('Sheet1').getRange(1,1,va.length,va[0].length).setValues(va);
}

我的图纸0:

COL1COL8COL9COL10
326
12107
425
10111
15414
11712
1507
141914
11816
0174
1093
3173
171611
81712
13018
1821
71716
15815
10139
10197

你知道。。。我只为…编码。。5天。

然而,我解决了我的问题,奇怪的是,在所有的回复中,没有人简单地说";将它们全部变成一个函数";,lol.

我所要做的就是把它变成一个函数,它提取一行的数据,创建文档,把文档保存到我告诉它的任何文件夹中,然后创建PDF,然后把它存储到我告诉他的文件夹中。接下来,我会让它以模板文档为主体自动发送电子邮件给PDF。如果有人想要代码,在这里…

function createDoc() {
console.log("pulling data and template");
var headers = Sheets.Spreadsheets.Values.get('1846CxmPdoc2VBW6GxaybCPW1_u2swO1jooIBiF2Yl90', 'log!A2:AA2');
var variables = Sheets.Spreadsheets.Values.get('1846CxmPdoc2VBW6GxaybCPW1_u2swO1jooIBiF2Yl90', 'log!A3:AA14');
var templateId = '1ONhT3n4Pr49BL6xEM_ykO9UVi8xZriA2fVAZjoFi2qI';
console.log("creating loop");
for (var i = 0; i < variables.values.length; i++) {
var client = variables.values[i][0];
var agent = variables.values[i][1];
var aaddress = variables.values[i][2];
var acity = variables.values[i][3];
var caddress = variables.values[i][4];
var ccity = variables.values[i][5];
var suopen = variables.values[i][6];
var suclose = variables.values[i][7];
var moopen = variables.values[i][8];
var moclose = variables.values[i][9];
var tuopen = variables.values[i][10];
var tuclose = variables.values[i][11];
var weopen = variables.values[i][12];
var weclose = variables.values[i][13];
var thopen = variables.values[i][14];
var thclose = variables.values[i][15];
var fropen = variables.values[i][16];
var frclose = variables.values[i][17];
var saopen = variables.values[i][18];
var saclose = variables.values[i][19];
var price = variables.values[i][20];
var appayment = variables.values[i][21];
var mpayment = variables.values[i][22];
var junepayment = variables.values[i][23];
var julypayment = variables.values[i][24];
var aupayment = variables.values[i][25];
var sepayment = variables.values[i][26];
console.log("setting document Id");
var documentId = DriveApp.getFileById('1ONhT3n4Pr49BL6xEM_ykO9UVi8xZriA2fVAZjoFi2qI').makeCopy().getId();
console.log("setting destinations")
var documentDestination = DriveApp.getFolderById('1YVhLzwZ9CI5-iTR1SKHF5ykNVqZQvQY9');
var pdfDestination = DriveApp.getFolderById("1_idXGdZo0l_U1IxuaLDUqrk0HjdfZvsg");

console.log("creating document");
DriveApp.getFileById(documentId).setName('2022' + " " + client + " " + 'Pool Management Proposal');
var document = DocumentApp.openById(documentId); 
var body = document.getBody();
body.replaceText('##Agent Name##', agent);
body.replaceText('##Agent Address##', aaddress);
body.replaceText('##Agent City/Zip##', acity);
body.replaceText('##Client Name##', client)
body.replaceText('##Client Address##', caddress);
body.replaceText('##Client City/Zip##', ccity);
body.replaceText('##Contract Price##', price);
body.replaceText('##April Payment##', appayment);
body.replaceText('##May Payment##', mpayment);
body.replaceText('##June Payment##', junepayment);
body.replaceText('##July Payment##', julypayment);
body.replaceText('##August Payment##', aupayment);
body.replaceText('##September Payment##', sepayment);
body.replaceText('##Sunday Open##', suopen);
body.replaceText('##Sunday Close##', suclose);
body.replaceText('##Monday Open##', moopen);
body.replaceText('##Monday Close##', moclose);
body.replaceText('##Tuesday Open##', tuopen);
body.replaceText('##Tuesday Close##', tuclose);
body.replaceText('##Wednesday Open##', weopen);
body.replaceText('##Wednesday Close##', weclose);
body.replaceText('##Thursday Open##', thopen);
body.replaceText('##Thursday Close##', thclose);
body.replaceText('##Friday Open##', fropen);
body.replaceText('##Friday Close##', frclose);
body.replaceText('##Saturday Open##', saopen);
body.replaceText('##Saturday Close##', saclose);
console.log("moving document");
var docFile = DriveApp.getFileById(documentId);
docFile.moveTo(documentDestination);

console.log("document creation complete");
document.saveAndClose();

console.log("converting document to PDF");
var templateFile = DriveApp.getFileById(documentId);
var folder = 
DriveApp.getFolderById('1_idXGdZo0l_U1IxuaLDUqrk0HjdfZvsg');
var theBlob = templateFile.getBlob().getAs('application/pdf');
var newPDFFile = folder.createFile(theBlob); 

console.log("naming PDF");
var fileName = templateFile.getName().replace(".", ""); //otherwise filename will be shortened after full stop   
newPDFFile.setName(fileName + ".pdf");
console.log("PDF creation complete");
}
}

最新更新