取消脚本单元格为红色

问题描述 投票:0回答:1

我有一个完美运行的脚本,但我有一些工作人员会运行该脚本,即使单元格是红色的,这是为了防止发送丢失的数据而设置的。

有没有办法在当前脚本中添加一行,这样如果存在红色单元格,它就不会运行?

我已经查看了各种建议,但不确定如何将该行添加到我当前的脚本中,以完美地满足我们的需要。

我的情况:

我有一个完美运行的脚本,但我有一些工作人员会运行该脚本,即使单元格是红色的,这是为了防止发送丢失的数据而设置的。

有没有办法在当前脚本中添加一行,这样如果存在红色单元格,它就不会运行?

当前脚本是:

function Email8() {
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();

var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName();

//var email = Session.getUser().getEmail();

var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin").getRange("G9:H10");

var emailAddress = emailRange.getValue();

var subject = "S/A - Darrin";

var body = ("Thank you for your business.");


var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
var shID = getSheetID("S/A - Darrin") //Get Sheet ID of sheet name "Master"
var url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=pdf&id="+ssID+"&gid="+shID;

var result = UrlFetchApp.fetch(url , requestData);  
var contents = result.getContent();

var bcc = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin").getRange("F5:G8").getDisplayValues().flat().join(",");var filename = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin").getRange("A21").getDisplayValue();
MailApp.sendEmail(emailAddress, subject, body, { attachments: [result.getBlob().setName(`${filename}.pdf`)], bcc });

var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();

  var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName();

  //var email = Session.getUser().getEmail();

  var email_ID1 = "[email protected]";
 
  var subject = "CTC Sales Agreement.";

  var body = ("Attached is your document. \n \Thank you for your business");


  var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
  var shID = getSheetID("Xero Invoice - Darrin") //Get Sheet ID of sheet name "Xero Invoice - Darrin"
  var url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=csv&id="+ssID+"&gid="+shID;

  var result = UrlFetchApp.fetch(url , requestData);  
  var contents = result.getContent();

  MailApp.sendEmail (email_ID1, subject ,body, {attachments:[{fileName:sheetName+".csv", content:contents, mimeType:"application//csv"}]});

};
function getSheetID(name){
var ss = SpreadsheetApp.getActive().getSheetByName(name)
var sheetID = ss.getSheetId().toString() 
return sheetID
}
javascript google-sheets google-apps-script colors triggers
1个回答
0
投票

使用 getBackgrounds()

根据您的帖子,如果有一个红色单元格(十六进制的

#ff0000
),您的脚本不应运行。您可以在脚本之前添加 if-else 语句,以便脚本在运行脚本之前首先检查具有红色背景的单元格。您可以使用以下示例作为脚本的基础:

function checkingForRed() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data = ss.getRange(1,1,10,5).getBackgrounds(); //Using 10 and 5 because my sample sheet has no data except for background colors green and red
  var checkForRed = data.filter(x=>x.includes('#ff0000')).length; //will return 0 if there is no red background color
  if (checkForRed) {
    console.log("Script will not run due to cells in red.");
    SpreadsheetApp.getActiveSpreadsheet().toast("Script will not run due to cells in red."); //toast will be visible in sheets ui
  }
  else {
    console.log("Running the script. Place your script inside the else.");
    SpreadsheetApp.getActiveSpreadsheet().toast("Running script."); //toast will be visible in sheets ui
  }
}

您可以在 else 语句中调用脚本,也可以在 else 语句中添加整个脚本。您可以修改第 3 行中的范围,并将第 10 和 5 更改为

ss.getLastRow()
ss.getLastColumn()
,因为我的测试表没有数据,仅看起来像这样:

sample sheet

如果我运行示例脚本,Google Apps 脚本执行日志应如下所示:

execution log

在这样的床单上有一个吐司通知:

toast notification

参考:

© www.soinside.com 2019 - 2024. All rights reserved.