簡體   English   中英

如果輸入日期已經存在於 Google 表格的受保護范圍內,如何避免阻止執行 Apps 腳本代碼

[英]How to avoid a block of Apps Script codes execution if an input date is already there in a protected range in Google Sheets

我必須滿足兩個條件才能執行一段代碼:

  1. 如果沒有該特定日期的數據,它將不會執行突出顯示的代碼塊 [大約。 帖子中的最后 30 行代碼]; 條件是 if(dates.indexOf(startDate).= -1){ 並且工作正常。
  2. 如果在 Google 表格的受保護數據范圍內已經存在輸入日期,它也不會執行相同的突出顯示的代碼塊。

輸入日期始終是當前日期,因此,我認為它應該只在最后一個受保護的數據范圍內進行搜索。

如果 refreshSheet() 和 onePeriod() 將一個接一個地執行,如何構建第二個條件並將其添加到第一個條件? 檢查附加的圖像也可以更好地了解問題。

    function onePeriod(){
      // For a single Period Class
      var spreadsheet = SpreadsheetApp.getActive();
      var dashboard = spreadsheet.getSheetByName("Dashboard");
      var sheetName = dashboard.getRange("A4").getValue();
      //retrieve the start date to use as desired
      var startDate = dashboard.getRange("C4").getDisplayValue();
      var endDate = dashboard.getRange("D4").getDisplayValue();
      var sheet = spreadsheet.getSheetByName(sheetName);
      //chose the range within the specified dates, for this first locate the date column
      var startRow = 2;
      var dateColumn = sheet.getRange(startRow,1,sheet.getLastRow(), 1);
      var dates = dateColumn.getDisplayValues().flat();
      var firstRow = dates.indexOf(startDate)+startRow;
      var lastRow = dates.lastIndexOf(endDate)+startRow;
      //now get the range between (and including) those rows
      var range = sheet.getRange(firstRow, 1, lastRow-firstRow+1, sheet.getLastColumn());
      
      //Sorting and removing duplicates
      // You need to specify by which column you want to sort your data, in this sample it it column 3 - that it column C  
      

**if(dates.indexOf(startDate) != -1){  
        var column = 3;
        range.sort({column: column, ascending:true});
        range.removeDuplicates([column]);
      
        //now delete empty rows if any
        var deleteRows = 0;  // <--- Added
        for (var i = range.getHeight(); i >= 1; i--){
            if(range.getCell(i, 1).isBlank()){
               sheet.deleteRow(range.getCell(i, 1).getRow());
               deleteRows++;
            }
        }
        
        //Protecting data 
        var timeZone = Session.getScriptTimeZone();
        var stringDate = Utilities.formatDate(new Date(), timeZone, 'dd/MM/yy HH:mm');
        var me = Session.getEffectiveUser();
        var description = 'Protected on ' + stringDate + ' by ' + me;
        var height = range.getHeight();
        var newHeight = height+1;
        var newRange = sheet.getRange(firstRow, 1, newHeight-deleteRows, sheet.getLastColumn());
      
        var protection = newRange.protect().setDescription(description);
        newRange.getCell(newHeight-deleteRows, 2).setValue(height-deleteRows + ' Students, Signed by ' + me).offset(0, -1, 1, 6).setBackground('#e6b8af');
        //protection.setDomainEdit(false);
        protection.addEditor(me);
        protection.removeEditors(protection.getEditors());
        if (protection.canDomainEdit()) {
          protection.setDomainEdit(false);
        }  
      }**
}
  • 您想為運行代碼塊創建附加條件
  • 條件是range不受保護
  • 要評估這種情況,您可以使用sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE)檢索工作表的所有受保護范圍,遍歷它們並驗證它們是否與當前范圍重疊(例如包含startRow
  • 如果存在range保護,則將 boolean 變量設置為true
  • if條件下使用 boolean

樣本:

...
  var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
  var protected = false;
  for (var k = 0; k < protections.length; k++){
    var protection = protections[k];
    if(firstRow >= protection.getRange().getRow() && firstRow <= protection.getRange().getLastRow()){
      //range is already protected
      protected = true;
      break;
    }
  }   
  if(dates.indexOf(startDate) != -1 && protected == false){
...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM