我正在嘗試為 Appsheet 應用程式撰寫 Javascript/appscript。我無法弄清楚這一點,如果有人可以幫忙。
我有三張表:專案子任務筆記
我希望能夠向專案所有者發送一封電子郵件,其中包括分配給該專案的所有任務的專案名稱(通過 PK/FK ID 的關系,專案名稱)以及分配給該專案的所有注釋(相同關系作為注釋 PK/FK ID,專案名稱),其中 last_update 列現在在 24 小時內。(即過去 24 小時內對任何任務或筆記所做的任何更改或創建)
我提供了所有表的樣本、它們的列和理想的樣本輸出
我的電子表格的公共鏈接:https : //docs.google.com/spreadsheets/d/1FcOKVkhdjK-vGuFPWSR2ZreBYFI6EK7EnQj3yKPvttk/edit?usp=sharing
理想輸出
uj5u.com熱心網友回復:
我相信你的目標如下。
您想通過從 3 個作業表中檢索值來創建電子郵件訊息,如下所示。(下圖來自您的問題。)

您想使用 Google Apps 腳本來實作這一點。
根據您對 的評論
Task order does not matter. Notes preferablly sorted in the email by most recent (not required),我認為您可能只想對“注釋”的值進行排序。
在這種情況下,以下示例腳本如何?
示例腳本:
請將以下腳本復制并粘貼到電子表格的腳本編輯器中。并且,請檢查作業表名稱。并請運行myFunction。
function myFunction() {
// Retrieve 3 sheets.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const [sheetITPM, sheetITPM_Tasks, sheetNotes] = ["ITPM", "ITPM_Tasks", "Notes"].map(e => ss.getSheetByName(e));
// Retrieve IDs, names and emails.
const values = sheetITPM.getRange("A2:J" sheetITPM.getLastRow()).getValues().map(r => ({ id: r[0], name: r[1], email: r[9] }));
// Retrieve Tasks.
const tasks = sheetITPM_Tasks.getRange("A2:G" sheetITPM_Tasks.getLastRow()).getValues().reduce((o, r) => (o[r[2]] = o[r[2]] ? [...o[r[2]], { description: r[3], status: r[4], dueDate: r[6] }] : [{ description: r[3], status: r[4], dueDate: r[6] }], o), {});
// Retrieve Notes.
const notes = sheetNotes.getRange("A2:E" sheetNotes.getLastRow()).getValues().reduce((o, r) => (o[r[1]] = o[r[1]] ? [...o[r[1]], { note: r[2], date: r[4] }] : [{ note: r[2], date: r[4] }], o), {});
Object.entries(notes).forEach(([, v]) => v.sort((a, b) => a.date.getTime() < b.date.getTime() ? 1 : -1)); // If you don't want to sort the values of "Notes", please remove this line.
// Send emails.
values.forEach(({ id, name, email }) => {
const message = [
`Here is the project update for: ${name}`,
"",
`Assigned Tasks:`,
...tasks[id].map(({ description, status, dueDate }, i) => [`Task ${i 1}:`, description, status, dueDate, ""].join("\n")),
"",
`Project Notes:`,
...notes[id].map(({ note }, i) => [`Note ${i 1}:`, note, ""].join("\n")),
].join("\n");
MailApp.sendEmail({ to: email, subject: "Project update", body: message });
});
}
- 運行此腳本時,通過從 3 個作業表中檢索值來發送電子郵件。電子郵件地址來自“ITPM”表的“J”列。
筆記:
- 此示例腳本適用于您的示例電子表格。因此,當電子表格的結構從您的示例電子表格更改時,此腳本可能無法使用。所以請注意這一點。測驗此腳本時,請使用此示例電子表格。
參考:
- 降低()
- forEach()
- 發送電子郵件(訊息)
添加:
OP的以下新問題,
是否可以讓它只包含列上次更新時間在 24 小時內的注釋和任務?
示例腳本:
function myFunction() {
// Retrieve 3 sheets.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const [sheetITPM, sheetITPM_Tasks, sheetNotes] = ["ITPM", "ITPM_Tasks", "Notes"].map(e => ss.getSheetByName(e));
// Retrieve IDs, names and emails.
const values = sheetITPM.getRange("A2:J" sheetITPM.getLastRow()).getValues().map(r => ({ id: r[0], name: r[1], email: r[9] }));
// Retrieve Tasks.
const tasks = sheetITPM_Tasks.getRange("A2:G" sheetITPM_Tasks.getLastRow()).getValues().reduce((o, r) => (o[r[2]] = o[r[2]] ? [...o[r[2]], { description: r[3], status: r[4], dueDate: r[6] }] : [{ description: r[3], status: r[4], dueDate: r[6] }], o), {});
// Retrieve Notes.
const notes = sheetNotes.getRange("A2:E" sheetNotes.getLastRow()).getValues().reduce((o, r) => (o[r[1]] = o[r[1]] ? [...o[r[1]], { note: r[2], date: r[4] }] : [{ note: r[2], date: r[4] }], o), {});
Object.entries(notes).forEach(([, v]) => v.sort((a, b) => a.date.getTime() < b.date.getTime() ? 1 : -1)); // If you don't want to sort the values of "Notes", please remove this line.
// By OP's new question, I added the following script.
const now = new Date().getTime();
const before24h = now - (24 * 60 * 60 * 1000);
Object.entries(tasks).forEach(([k, v]) => {
tasks[k] = v.filter(({dueDate}) => {
const temp = dueDate.getTime();
return now < temp && temp > before24h;
});
});
Object.entries(notes).forEach(([k, v]) => {
notes[k] = v.filter(({date}) => {
const temp = date.getTime();
return now < temp && temp > before24h;
});
});
// Send emails.
values.forEach(({ id, name, email }) => {
const message = [
`Here is the project update for: ${name}`,
"",
`Assigned Tasks:`,
...tasks[id].map(({ description, status, dueDate }, i) => [`Task ${i 1}:`, description, status, dueDate, ""].join("\n")),
"",
`Project Notes:`,
...notes[id].map(({ note }, i) => [`Note ${i 1}:`, note, ""].join("\n")),
].join("\n");
MailApp.sendEmail({ to: email, subject: "Project update", body: message });
});
}
- 但是,關于您的新問題,當您使用示例電子表格時,在這種情況下,
tasks并且notes沒有值。請注意這一點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/372900.html
標籤:javascript 电子邮件 谷歌应用程序脚本 谷歌表格
上一篇:串列索引沒有回傳值
