0
私はちょっとしたやりかたの後で、自分のニーズに合った素敵なPDF差し込み印刷のGoogle Appスクリプトを手に入れました。私の問題は今、「Mail Merge PDF」機能をスプレッドシートのすべての行で実行したいと思います。配列を使って効率的な方法があると思います。この時点で、私はちょっと困惑しています。JavaScriptを使用して、動作しているGoogle App Scriptをループする必要があります。 (PDF差し込み印刷)
私はできるだけコードを再利用できるように文書化しようとしました。スクリプトは現在の状態で正常に動作していますが、1行の処理に限定されています。
よろしく New_2_Code
// Create Custom Scripts Menu in the Google Sheet this script resides in and where the data is housed.
function onOpen() {
var menuEntries = [{
name: "Long Service Award Generator",
functionName: "PDFMailMerge"
}];
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.addMenu("Custom Scripts", menuEntries);
}
function PDFMailMerge() {
// Enter the ID of the Google Docs Document you wish to use as a Template for your Document.
var sleepINT = 1500
var templateid = "1g0-ydpw_ZsKjcRZ_yKgaqHFFf56DmP4orA1t1MNFpPA"; // template file id
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.toast("10%: Template Sheet Aquired.");
Utilities.sleep(sleepINT);
var sheet = ss.getActiveSheet();
var data = sheet.getRange(2, 1, 1, 15).getValues();
ss.toast("25%: Template data captured.");
Utilities.sleep(sleepINT);
// Make a copy of the Long Service Award template, then Fill up it up with the data from the Long Service Award Spreadsheet.
for (var i in data) {
var row = data[i];
var docid = DriveApp.getFileById(templateid).makeCopy().getId();
var doc = DocumentApp.openById(docid);
var body = doc.getActiveSection();
body.replaceText("%Preferred_Name%", row[0]);
body.replaceText("%Last_Name%", row[1]);
body.replaceText("%Emp_No%", row[2]);
body.replaceText("%EMAIL%", row[3]);
body.replaceText("%Personnel_Sub_Area_Desc%", row[4]);
body.replaceText("%Personnel_Sub_Area_Desc%", row[5]);
doc.saveAndClose();
}
ss.toast("40%: Template data has been replaced.");
Utilities.sleep(sleepINT);
//Specifiy a Google Drive Folder by it's URL ID below.
//Create a duplicate of the newly modified Template to the specified folder, then delete the originally modified Template from the specified folder.
var file = DriveApp.getFileById(doc.getId());
var newfolder = DriveApp.getFolderById("1x3uJOuXvKMeoZpWXV7g5nJ_tujMXymaO");
var oldfolder = DriveApp.getFolderById("1x3uJOuXvKMeoZpWXV7g5nJ_tujMXymaO");
newfolder.addFile(file);
oldfolder.removeFile(file);
ss.toast("60%: Template has been put in correct folder.");
Utilities.sleep(sleepINT);
//Customizing the Template Title
var usernamefordoctitle = sheet.getRange(2, 1, 1, 1).getValues() // this is grabbing the customer name field (A2)
var name = doc.getName();
doc.setName('Template Name for ' + usernamefordoctitle);
ss.toast("70%: named new Long Service Award");
Utilities.sleep(sleepINT);
//Set a Google Drive Folder's URL ID below, .PDF Files generated through this method will be stored in this folder.
//Create a PDF File using the Modified Template created earlier in the script and name the resulting .PDF file according to the Template Title
var pdffolder = DriveApp.getFolderById("1x3uJOuXvKMeoZpWXV7g5nJ_tujMXymaO");
var pdfFILE = DriveApp.getFileById(doc.getId()).getAs('application/pdf');
pdfFILE.setName(doc.getName() + ".pdf");
var theFolder = pdffolder;
var theFile = DriveApp.createFile(pdfFILE);
theFolder.addFile(theFile);
ss.toast("80%: PDF generated");
Utilities.sleep(sleepINT);
//Send an email to recepient (Email Values in Colmun D) - Attatch PDF File Created Earlier.
var pdfEMAIL = DriveApp.getFileById(doc.getId()).getAs('application/pdf').getBytes();
var message = "Hi " + usernamefordoctitle + "!, please kindly find your invoice attached.\nMany Thanks!\nMe";
var emailAdd = sheet.getRange("D2").getValue()
var emailTo = emailAdd; // add customer email here
var subject = "Long Service Award for " + usernamefordoctitle;
var attach = {
fileName: 'Long Service Award for ' + usernamefordoctitle + '.pdf',
content: pdfEMAIL,
mimeType: 'application/pdf'
};
MailApp.sendEmail(emailTo, subject, message, {
attachments: [attach]
});
ss.toast("90%: " + usernamefordoctitle + "has been emailed.");
Utilities.sleep(sleepINT);
ss.toast("100%: Template for " + usernamefordoctitle + " has been created. Doc ID: " + docid);
Utilities.sleep(sleepINT);
}
UPDATE: 私は、forループのネストされた実装しようとしています。私はその時点で大成功を収めていません。スクリプトはエラーなく実行されますが、最初の行だけが処理されます。 ( "sheet.getLastRow()"の長さに等しい)
あまりフォーマットされていないコードについては、事前にお詫び申し上げます。私は焼き尽くされ、正しく編集できません。
コードは以下の通りです:
は// Create Long Service Award Menu in Google Sheet this script resides in and where the data is housed.
function onOpen() {
var menuEntries = [ {name: "Test", functionName: "PDFMailMerge"}];
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.addMenu("Custom Scripts", menuEntries);
}
function PDFMailMerge() {
//Super Loop
var ss2 = SpreadsheetApp.getActiveSpreadsheet();
var sheet2 = ss2.getActiveSheet();
for(var index2 = 1; index2 < sheet2.getLastRow(); index2++)
{
var data2 = sheet2.getRange(index2,1,1,15).getValues();
for (var i in data2) {
var row = data2[i];
//your code here
// Enter the ID of the Google Docs Document you wish to use as a Template for your Document.
var sleepINT = 1500
var templateid = "1g0-ydpw_ZsKjcRZ_yKgaqHFFf56DmP4orA1t1MNFpPA"; // template file id
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.toast("10%: Template Sheet Aquired.");
Utilities.sleep(sleepINT);
var sheet = ss.getActiveSheet();
var data = sheet.getRange(2,1,1,15).getValues();
ss.toast("25%: Template data captured.");
Utilities.sleep(sleepINT);
if (index2 < sheet.getLastRow()){
for (var j = 0; j < sheet.getLastRow(); j++)
{var data = sheet.getRange(1+j,1,1,15).getValues();
j++}
}
else {
ss.toast("Error")
}
//var data needs to increment
// Make a copy of the Long Service Award template, then Fill up it up with the data from the Long Service Award Spreadsheet.
for (var i in data) {
var row = data[i];
var docid = DriveApp.getFileById(templateid).makeCopy().getId();
var doc = DocumentApp.openById(docid);
var body = doc.getActiveSection();
body.replaceText("%Preferred_Name%", row[0]);
body.replaceText("%Last_Name%", row[1]);
body.replaceText("%Emp_No%", row[2]);
body.replaceText("%%Personnel_Sub_Area%%", row[4]);
body.replaceText("%Personnel_Sub_Area_Desc%", row[5]);
doc.saveAndClose();
}
ss.toast("40%: Template data has been replaced.");
Utilities.sleep(sleepINT);
//Specifiy a Google Drive Folder by it's URL ID below.
//Create a duplicate of the newly modified Template to the specified folder, then delete the originally modified Template from the specified folder.
var file = DriveApp.getFileById(doc.getId());
var newfolder = DriveApp.getFolderById("1x3uJOuXvKMeoZpWXV7g5nJ_tujMXymaO");
var oldfolder = DriveApp.getFolderById("1x3uJOuXvKMeoZpWXV7g5nJ_tujMXymaO");
newfolder.addFile(file);
oldfolder.removeFile(file);
ss.toast("60%: Template has been put in correct folder.");
Utilities.sleep(sleepINT);
if (index2 < sheet.getLastRow()){
for (var p = 1; p < sheet.getLastRow(); p++)
{var usernamefordoctitle = sheet.getRange(2, 1, 1, 1).getValues();
p++}
// Column in range in usernamefordoctitle must increment up
//Customizing the Template Title
var usernamefordoctitle = sheet.getRange(2, 1, 1, 1).getValues() // this is grabbing the customer name field (A2)
var name = doc.getName();
doc.setName('Template Name for ' + usernamefordoctitle);
ss.toast("70%: named new Long Service Award");
Utilities.sleep(sleepINT);
}
else {
var usernamefordoctitle = sheet.getRange(2, 1, 1, 1).getValues() // this is grabbing the customer name field (A2)
var name = doc.getName();
doc.setName('Template Name for ' + usernamefordoctitle);
ss.toast("70%: named new Long Service Award");
Utilities.sleep(sleepINT);
}
//Does not need incrememnting - The value for usernamefordoctitle is used here which is incrememented
//Set a Google Drive Folder's URL ID below, .PDF Files generated through this method will be stored in this folder.
//Create a PDF File using the Modified Template created earlier in the script and name the resulting .PDF file according to the Template Title
var pdffolder = DriveApp.getFolderById("1x3uJOuXvKMeoZpWXV7g5nJ_tujMXymaO");
var pdfFILE = DriveApp.getFileById(doc.getId()).getAs('application/pdf');
pdfFILE.setName(doc.getName() + ".pdf");
var theFolder = pdffolder;
var theFile = DriveApp.createFile(pdfFILE);
theFolder.addFile(theFile);
ss.toast("80%: PDF generated");
Utilities.sleep(sleepINT);
if (index2 < sheet.getLastRow()){
for (var k = 0; k < sheet.getLastRow(); k++)
{var data = sheet.getRange(1+k,1,1,15).getValues();
k++}
}
else {
ss.toast("Error")
}
//email add needs to be changed to get value and be incrememented
//Send an email to recepient (Email Values in Colmun D) - Attatch PDF File Created Earlier.
var pdfEMAIL = DriveApp.getFileById(doc.getId()).getAs('application/pdf').getBytes();
var message = "Hi " + usernamefordoctitle + "!, please kindly find your invoice attached.\nMany Thanks!\nMe";
var emailAdd = sheet.getRange("D2").getValue()
var emailTo = emailAdd; // add customer email here
var subject = "Long Service Award for " + usernamefordoctitle;
var attach = {fileName:'Long Service Award for ' + usernamefordoctitle + '.pdf',content:pdfEMAIL, mimeType:'application/pdf'};
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
ss.toast("90%: " + usernamefordoctitle + "has been emailed.");
Utilities.sleep(sleepINT);
ss.toast("100% Template for " + usernamefordoctitle + " has been created. Doc ID: ");
Utilities.sleep(sleepINT);
}
}
}
こんにちはLiora、あなたが私を与えたあなたのスニペットは十分に動作するようです。それは私が探していたものである行ごとにスクリプトを1回実行します。問題は今でも最初の行だけを処理しています。私は次の行からデータを読み込むためにスクリプトが必要なセクションで、あなたと同様のforループを使用できると思っています。私はそれを手がかりにして、いったん作業バージョンを取得すると、誰もが利用できるようにここに投稿します。 (あなたの助けをもう一度ありがとう。) –