我有這個代碼(謝謝你,Tanaike)。我遇到的問題。即使24小時內沒有任務更新和筆記更新,它是否也會向專案所有者發送電子郵件。所以他們收到空白電子郵件。我正在嘗試做的是,如果沒有任務更新且沒有更新 - 不要發送電子郵件
電子表格的公共鏈接:
function ProjectUpdate24hour() {
// Retrieve our 3 needed sheets.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const [sheetITPM, sheetITPM_Tasks, sheetNotes] = ["ITPM", "ITPM_Tasks", "Notes"].map(e => ss.getSheetByName(e));
// Retrieve Project 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 All Tasks.
const tasks = sheetITPM_Tasks.getRange("A2:H" sheetITPM_Tasks.getLastRow()).getValues().reduce((o, r) => (o[r[2]] = o[r[2]] ? [...o[r[2]], { description: r[3], status: r[4], owner: r[5], dueDate: r[6], t_lastupdate: r[7] }] : [{ description: r[3], status: r[4], owner: r[5], dueDate: r[6], t_lastupdate: r[7] }], o), {});
// Retrieve All Notes.
const notes = sheetNotes.getRange("A2:F" sheetNotes.getLastRow()).getValues().reduce((o, r) => (o[r[1]] = o[r[1]] ? [...o[r[1]], { note: r[2], author: r[3], date: r[4], n_lastupdate: r[5] }]
: [{ note: r[2], author: r[3], date: r[4], n_lastupdate: r[5] }], o), {});
Object.entries(notes).forEach(([, v]) => v.sort((a, b) => a.n_lastupdate.getTime() < b.n_lastupdate.getTime() ? 1 : -1)); //Sort Notes by latest update
//This determines if the tasks lastest update column is within 25 hours of the current time
const now = new Date().getTime();
const before24h = now - (25 * 60 * 60 * 1000);
Object.entries(tasks).forEach(([k, v]) => {
tasks[k] = v.filter(({t_lastupdate}) => {
const temp = t_lastupdate.getTime();
return now > temp && temp > before24h;
});
});
//This determiens if the notes lastest update column is within 25 hours of the current time
Object.entries(notes).forEach(([k, v]) => {
notes[k] = v.filter(({n_lastupdate}) => {
const temp = n_lastupdate.getTime();
return now > temp && temp > before24h;
});
});
// Send our emails to project owners with our email format
values.forEach(({ id, name, email }) => {
const message = [
`Here is the 24 hour project update for Project: \n${name}`,
"",
`Assigned Tasks:`,
...tasks[id].map(({ description, status, owner, dueDate }, i) => [`Task ${i 1}:`, `Task Description: ${description}`,`Task Owner: ${owner}`,`Task Status: ${status}`,
`Task Due Date: ${dueDate}`, ""].join("\n")),
`Project Notes:`,
...notes[id].map(({ note,author }, i) => [`Note ${i 1}: ${note}`,`Author: ${author}`, ""].join("\n")),
].join("\n");
MailApp.sendEmail({ to: email, subject: "Project Update", body: message });
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
uj5u.com熱心網友回復:
我相信你的目標如下。
- 你不想當這兩個值,以發送電子郵件
tasks,并notes沒有任何價值。
在這種情況下,如何進行以下修改?
從:
values.forEach(({ id, name, email }) => {
const message = [
到:
values.forEach(({ id, name, email }) => {
if (tasks[id].length == 0 && notes[id].length == 0) return; // Added
const message = [
- 通過這一修改,當這兩個值
tasks,并notes沒有值,不發送電子郵件。
筆記:
如果您不想在其中之一
tasks和notes沒有值時發送電子郵件,請修改如下。if (tasks[id].length == 0 || notes[id].length == 0) return;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/383757.html
標籤:javascript 功能 谷歌应用程序脚本 谷歌表格
上一篇:當代碼在其他檔案中時如何運行Cron作業-NodeJS
下一篇:如何從另一個函式訪問布爾陣列?C
