我解決了以下問題。使用 Google Sheets 的 Google Script 服務向所有人發送 PDF 檔案。我嘗試向串列中的每個人發送一個檔案。進展順利。
但是,我在嘗試制作類似的腳本時遇到了問題,但是為了將多個檔案發送給 Google 表格串列中的不同人。每個人都有不同的檔案。
例如:
John 有三個 PDF 檔案:John_999901.pdf、John_999902.pdf 和 John_999903.pdf。
David 有兩個 PDF 檔案:David_ 999901.pdf 和 David_99990。
Jun 有兩個 PDF 檔案:Jun_999901.pdf 和 Jun_999902.pdf。
最后,米歇爾只有一個 PDF 檔案:Michel_999901.pdf。
所有 PDF 檔案都保存在 GOOGLE DRIVE 上。
這是我保存 PDF 檔案的地方
這是我的電子表格
有沒有辦法根據串列中同名人員對應的檔案名發送電子郵件?
下面是我嘗試發送檔案的代碼
function onOpen()
{
var ui = SpreadsheetApp.getUi();
ui.createMenu('メール送信')
.addItem('送信', 'sendFormToAll')
.addToUi();
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
function sendFormToAll()
{
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var last_row = sheet.getDataRange().getLastRow();
for(var row=2; row <= last_row; row )
{
sendEmailWithAttachment(row);
sheet.getRange(row,8).setValue("send");
}
}
function sendPDFForm()
{
var row = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
sendEmailWithAttachment(row);
}
function sendEmailWithAttachment(row)
{
var filename= '?????.pdf';// I don't know what to do at this point
var file = DriveApp.getFilesByName(filename);
if (!file.hasNext())
{
console.error("Could not open file " filename);
return;
}
var client = getClientInfo(row);
var template = HtmlService
.createTemplateFromFile('index');
template.client = client;
var message = template.evaluate().getContent();
MailApp.sendEmail({
to: client.email,
subject: "Send File",
htmlBody: message ,
attachments: [file.next().getAs(MimeType.PDF)]
});
}
function getClientInfo(row)
{
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var values = sheet.getRange(row,1,row,8).getValues();
var rec = values[0];
var client =
{
name:rec[0],
email: rec[1]
};
client.name = client.name;
return client;
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
</head>
<body>
<div >
<p>Hi, <?= client.name ?>Sir </p>
<p>I send you file . Please check it</p>
<p>*********************************************************</p>
</div>
</body>
</html>
uj5u.com熱心網友回復:
在您的腳本中,以下修改如何實作您的目標?
改裝要點:
- 當
getValue在一個回圈中被使用,該方法成本變高。 - 需要添加從檔案夾中的檔案中搜索檔案名的邏輯。
- 當我看到您的示例電子表格時,
Situation是“C”列。但是當我看到你的腳本時,它是sheet.getRange(row,8).setValue("send");. 如果Situation與 相同sheet.getRange(row,8).setValue("send");,則必須為sheet.getRange(row,3).setValue("send");。但我不確定你的實際情況。所以在這個修改中,我使用sheet.getRange(row,3).setValue("send");了你的示例電子表格。 - 在您的腳本中,
Sheet1用作作業表名稱。但是在您的示例電子表格中,作業表名稱是Request. 在此修改中,我用于Request您的示例電子表格。
當這些點反映到你的腳本中時,它變成如下。
修改后的腳本:
請將您的檔案夾 ID 設定為###of DriveApp.getFolderById("###").searchFiles(`title contains '${name}' and trashed=false`);。
function sendFormToAll() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Request');
var values = sheet.getRange("A2:C" sheet.getLastRow()).getValues();
var rangeList = values.reduce((ar, e, i) => {
if (e[2] == "Send File") return ar;
var [name, email] = e.map(f => f.trim());
var files = DriveApp.getFolderById("###").searchFiles(`title contains '${name}' and trashed=false`);
var attachments = [];
var r = new RegExp(`^${name}[_\\d\\s] \\.pdf\$`, "i");
while (files.hasNext()) {
var file = files.next();
if (r.test(file.getName())) attachments.push(file.getBlob());
}
if (attachments.length == 0) {
console.error("Could not find files related to " name);
return ar;
}
var template = HtmlService.createTemplateFromFile('index');
template.client = { name };
var message = template.evaluate().getContent();
MailApp.sendEmail({ to: email, subject: "Send File", htmlBody: message, attachments });
ar.push(`C${i 2}`);
return ar;
}, []);
if (rangeList.length == 0) return;
sheet.getRangeList(rangeList).setValue("Send File");
}
筆記:
- 此示例腳本適用于您的示例電子表格。因此,如果實際電子表格的結構與示例不同,則可能無法使用此腳本。所以,請注意這一點。
參考:
- 減少()
- 搜索檔案(引數)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/389696.html
