簡體   English   中英

根據 Google Sheet 腳本第一列合並單元格中的日期從工作表鎖定異常中刪除行

[英]Remove rows from sheet lock exception based on date in merged cell in first column in Google Sheet script

這是對這個問題和 Marios 的 anser 的后續行動。 我對代碼進行了一些更改,以更改包括今天在內的所有日期的格式,而不僅僅是今天。 添加更多上下文,我現在的代碼是:

const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getSheetByName("Sheet1");
var protection = sh.protect().setDescription('Registration closed');
var unprotected = sh.getRange(6, 2, 18, 6);
protection.setUnprotectedRanges([unprotected]);

function myFunction() {
  
  const today = (new Date()).toLocaleString("sv-SE").slice(0,10);
  const range = sh.getRange("A6:A"+sh.getLastRow());
  const mergedRanges = range.getMergedRanges();
  
  mergedRanges.forEach(mR=> 
     {                  
     temp_vals = mR.getValues().flat();
     cell_date = (new Date(temp_vals[0]).toLocaleString("sv-SE").slice(0,10));
  
     if(today >= cell_date) {
       sh.getRange(mR.getRow(),mR.getColumn(),mR.getHeight(),6).setBackground('#FF8C00');
     }
     });
}

來自 Marios 的代碼成功地格式化了第 18-23 行。 我在除了幾行之外被鎖定的工作表上運行這個腳本。 我現在還嘗試鎖定彩色單元格以避免編輯,從而有效地刪除這些行的異常。 我試圖插入

var unprotected = regsheet.getRange(mR.getRow(),mR.getColumn(),mR.getHeight(),7);

進入 if 語句的 else 條件(並將保護代碼移到函數之后),但我認為這個變量在循環中被覆蓋,所以它不是一個可以不受保護的范圍。

我的工作表如下所示:

在此處輸入圖片說明

解釋:

  • 以下腳本將防止在着色后編輯新的着色單元格范圍 它利用了保護類。
  • 已經鎖定的范圍將保持鎖定狀態
  • 未鎖定且在腳本運行當天不需要着色的范圍將保持解鎖狀態

解決方案:

function myFunction() {
  
  const today = (new Date()).toLocaleString("sv-SE").slice(0,10);
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sh = ss.getSheetByName("Sheet1");
  const range = sh.getRange("A1:A"+sh.getLastRow());
  const mergedRanges = range.getMergedRanges();
   
  mergedRanges.forEach(mR=> 
     {                  
     temp_vals = mR.getValues().flat();
     cell_date = (new Date(temp_vals[0]).toLocaleString("sv-SE").slice(0,10));
  
     if(today === cell_date) {
       var rg = sh.getRange(mR.getRow(),mR.getColumn(),mR.getHeight(),6);
       rg.setBackground('#FF8C00');
 
       const protection = rg.protect().setDescription('Registration closed');
       const me = Session.getEffectiveUser();
       protection.addEditor(me);
       protection.removeEditors(protection.getEditors());
       if (protection.canDomainEdit()) {
              protection.setDomainEdit(false);
       }       
     }});
}

暫無
暫無

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

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