我有一个google工作表,每天根据今天的日期填充数据,我想做的是每天在某个时间触发条件下将每条包含数据的行推送到Slack频道中。如您所见,我开始构建此模型,以使每个单元格都有自己的变体,但是当第3行没有数据时,它仍会将空白更新推送到Slack中,如下所示:
我猜想我需要在这里的某个地方添加一个IF,但不确定在哪里。这是代码:
function createMeetingMessage(sheet) {
var porow1 = "Bulk!A2";
var OrderDaterow1 = "Bulk!K2";
var OrderReqrow1 = "Bulk!J2";
var Desrow1 = "Bulk!D2";
var Vendorrow1 = "Bulk!E2";
var ETArow1 = "Bulk!L2";
var porow2 = "Bulk!A3";
var OrderDaterow2 = "Bulk!K3";
var OrderReqrow2 = "Bulk!J3";
var Desrow2 = "Bulk!D3";
var Vendorrow2 = "Bulk!E3";
var ETArow2 = "Bulk!L3";
var porow3 = "Bulk!A4";
var OrderDaterow3 = "Bulk!K4";
var OrderReqrow3 = "Bulk!J4";
var Desrow3 = "Bulk!D4";
var Vendorrow3 = "Bulk!E4";
var ETArow3 = "Bulk!L4";
var message = "> *Today's Purchases*" +"\n"
+ "> *PO:* " +sheet.getRange(porow1).getValue() +"\n"
+ "> *Order Date:* " +sheet.getRange(OrderDaterow1).getValue() +"\n"
+ "> *Requested By:* " +sheet.getRange(OrderReqrow1).getValue() +"\n"
+ "> *Description:* " +sheet.getRange(Desrow1).getValue() +"\n"
+ "> *Vendor:* " +sheet.getRange(Vendorrow1).getValue() +"\n"
+ "> *ETA:* " +sheet.getRange(ETArow1).getValue() +"\n"
+"\n"
+ "> *PO:* " +sheet.getRange(porow2).getValue() +"\n"
+ "> *Order Date:* " +sheet.getRange(OrderDaterow2).getValue() +"\n"
+ "> *Requested By:* " +sheet.getRange(OrderReqrow2).getValue() +"\n"
+ "> *Description:* " +sheet.getRange(Desrow2).getValue() +"\n"
+ "> *Vendor:* " +sheet.getRange(Vendorrow2).getValue() +"\n"
+ "> *ETA:* " +sheet.getRange(ETArow2).getValue() +"\n"
+"\n"
+ "> *PO:* " +sheet.getRange(porow3).getValue() +"\n"
+ "> *Order Date:* " +sheet.getRange(OrderDaterow3).getValue() +"\n"
+ "> *Requested By:* " +sheet.getRange(OrderReqrow3).getValue() +"\n"
+ "> *Description:* " +sheet.getRange(Desrow3).getValue() +"\n"
+ "> *Vendor:* " +sheet.getRange(Vendorrow3).getValue() +"\n"
+ "> *ETA:* " +sheet.getRange(ETArow3).getValue() +"\n";
return message;
}
function triggerSlackRequestPD(channel, msg) {
var slackWebhook = "[SLACK WEBHOOK]";
var payload = { "channel": channel, "text": msg, "link_names": 1, "username": "Procurementron", "icon_emoji": ":robot_face:" };
var options = { "method": "post", "contentType": "application/json", "muteHttpExceptions": true, "payload": JSON.stringify(payload) };
Logger.log(UrlFetchApp.fetch(slackWebhook, options));
}
function notifySlackChannelOfFacilitatorAndNoteTaker() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Bulk");
if (!sheet.getRange("B2").getValue()) return;
var channel = getChannel();
var msg = createMeetingMessage(sheet);
triggerSlackRequestPD(channel, msg);
}
function isTest() {
return false;
}
function getChannel() {
if(isTest()) {
return "[SLACK CHANNEL]";
} else {
return "[SLACK CHANNEL]";
}
}
打开所有建议。理想情况下,在列中有一个类似var get.alldata的变量,这样我就不必为每个单元格做一个变体。谢谢!
答案 0 :(得分:2)
porow3, OrderDaterow3, OrderReqrow3, Desrow3, Vendorrow3, ETArow3
为空时,您不想将它们包括在message
中。如果我的理解是正确的,那么该修改如何?我认为针对您的情况有几种解决方案。因此,请将此视为其中之一。
createMeetingMessage()
对其进行修改。createMeetingMessage()
,所有值都是恒定范围,每条消息的所有标签也相同。
getValue()
检索值。
getValues()
检索所有值。当以上几点反映到您的脚本时,脚本如下。
请将您的createMeetingMessage()
修改为以下脚本,然后尝试。
function createMeetingMessage(sheet) {
var values = sheet.getRange("Bulk!A2:L4").getValues(); // Please modify this range, if you want to add rows.
var message = values.reduce(function(s, e) {
var porow, OrderDaterow, OrderReqrow, Desrow, Vendorrow, ETArow, _,
[porow, _, _, Desrow, Vendorrow, _, _, _, _, OrderReqrow, OrderDaterow, ETArow] = e;
if (porow && OrderDaterow && OrderReqrow && Desrow && Vendorrow && ETArow) {
s += "> *PO:* " + porow +"\n"
+ "> *Order Date:* " + OrderDaterow +"\n"
+ "> *Requested By:* " + OrderReqrow +"\n"
+ "> *Description:* " + Desrow +"\n"
+ "> *Vendor:* " + Vendorrow +"\n"
+ "> *ETA:* " + ETArow +"\n"
+"\n";
}
return s;
}, "> *Today's Purchases*" +"\n");
return message;
}
porow, OrderDaterow, OrderReqrow, Desrow, Vendorrow, ETArow
的所有值均存在,它们包含在消息中。
Bulk!A2:L4
中的var values = sheet.getRange("Bulk!A2:L4").getValues()
。