如何在Google表格中填充后保护一行?

时间:2017-08-07 20:48:50

标签: google-sheets protection

我已经在这里的某个人的帮助下编写了一些代码并进行了一些研究。该表是任何有权访问该链接的人的协作表。无需登录。

脚本执行此操作:在完全填充行后,该行的电子邮件将以特定格式发送到特定电子邮件。发送电子邮件时,该行的最后一个单元格将填充“EMAIL_SENT”。该脚本有一个功能,如果它在每行的最后一个单元格中看到“EMAIL_SENT”,则不会再次发送该行。脚本按时间触发,每分钟运行一次。

我现在需要帮助的是保护已发送的行。换句话说,如果一行的最后一个单元格填充了“EMAIL_SENT”,那么我希望保护该行不被我以外的任何人编辑。

这是我的脚本 -

// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";

function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1000;   // Number of rows to process
  // Fetch the range of cells A2:K2
  var dataRange = sheet.getRange(2,1,1000,14)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();

for (var i = 0; i < data.length; ++i) {
   if(testData(data[i])){ // Pass the row of data to the testData function (testData will return true or false)
    var row = data[i];
    var emailAddress = row[0];  // First column
    var message  = Utilities.formatDate(row[1], "GMT" , "MM/dd/yyyy" );
    var message1 = row[2];    // third column
    var message2 = row[3];
    var message3 = row[4];
    var message4 = row[5];
    var message5 = row[6];
    var message6 = row[7];
    var message7 = row[8];
    var message8 = row[9];
    var message9 = row[10];
    var message10 = row[11];
    var message11 = Utilities.formatDate(row[12], "GMT" , "MM/dd/yyyy" );
    var emailSent = row[13];    // 14th column
    if (emailSent != EMAIL_SENT) {  // Prevents sending duplicates
      var subject = "NEW SALE!!- " + row[4] + "- " + row[3];
      MailApp.sendEmail(emailAddress, subject, "\nSALE DATE: " + message + "\nCUSTOMER NAME: " + message1 + "\nJOB#: " + message2 + "\nModel: " + message3 + "\nLENDER/CASH: " + message4 + "\nAGED INVENTORY: " + message5 + "\nREPLACING A CAN: " + message6 + "\nCustomer Referral: " + message7 + "\nNHC Referral: " + message8 + "\nFN SALE: " + message9 + "\nSOURCE: " + message10 + "\nCLOSING DATE: " + message11 + "\n" + "\n" + "\n***Please do not reply to this message.***");
      sheet.getRange(startRow + i, 14).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();     
    }
  }
 }
}


function testData(data) { // Function to test the data 
    for (var x = 0; x < data.length - 1; x++) { // Loop through each cell (length -1 because we don't want to check the email sent cell as that should be empty)
        if (!data[x]) { // If a cell is empty return false
            return false;
        }
    }
    return true; // If no empty cells are found return true
}

0 个答案:

没有答案