我看了你的第二个链接并复制了一份好奇......如果你能处理一部分工作是手动的,你想做的似乎很容易实现,即获取您要添加的所有附件。
也可以导入这些文件并将它们中的每一个分配给脚本中的每封电子邮件,但这会使整个事情变得更加复杂。
假设您已准备好获取 ID,您应该在“要合并的数据”表 (col F) 中添加第 5 列,并按如下方式更改脚本(我仅从第 70 行开始复制相关部分function runMailMerge() {
:)
function runMailMerge() {
//set up the status column, so it's blank, and pink
var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data to be merged"); //used later in updating the status box
var totalRows=sheet.getMaxRows();
var range=sheet.getRange(2,1,totalRows,1);
var failed=false;
range.clearContent();
range.setBackgroundRGB(230,153,153);
if (totalRows>25) Browser.msgBox("It will take some time to send all these emails. There are more than 25 rows, so we're using the slower mode, to send as many as possible. See the Instructions and Settings sheet for details.");
ScriptProperties.setProperty('newLine',"<br><br>");
settingsDataRange=setProperties();
var mergeDataValues=getMergeDataValues();
var arrayOfTags=mergeDataValues[0]; //gets the first row
//run through the remaining rows
for (i=1; i<mergeDataValues.length; i++) { //loops through the rows in the merge sheet. Misses 0, which is column headings
try {
var thisEmailAddress=mergeDataValues[i][2];
var thisEmailText=ScriptProperties.getProperty('genericEmailText');
var thisEmailSubject=ScriptProperties.getProperty('subject');
var arrayOfValues=mergeDataValues[i];
for (j=1; j<arrayOfTags.length; j++) { //loops through the columns
var replaceData=arrayOfValues[j];
var tag=ScriptProperties.getProperty('before')+arrayOfTags[j]+ScriptProperties.getProperty('after');
while (thisEmailText.search(tag)>=0) { //loop through, to ensure we replace every occurence of "tag"
thisEmailText=thisEmailText.replace(tag,arrayOfValues[j]);
thisEmailSubject=thisEmailSubject.replace(tag,arrayOfValues[j]);
}
}
thisEmailText=thisEmailText.replace(/\n/g,"<br>"); //ensure any linebreaks from Google doc are carried to the HTML version. The /g means all occurences.
//create a plain text version, by swapping <br> for \n, and stripping any other HTML tags
var plainEmailText=thisEmailText.replace(/<br>/g,"\n");
plainEmailText=stripHTML(plainEmailText);
var attachmentFiles = [];
var ID = sheet.getRange(i+1,5).getValue();
Logger.log(ID);
var pdf = DocsList.getFileById(ID);
attachmentFiles.push(pdf);
var advancedArgs = {htmlBody:thisEmailText, name:ScriptProperties.getProperty('senderName'), replyTo:ScriptProperties.getProperty('replyTo'), attachments:attachmentFiles };
MailApp.sendEmail(thisEmailAddress,thisEmailSubject, plainEmailText, advancedArgs);
var range=sheet.getRange(i+1,1);
...