![](/img/trans.png)
[英]Google Sheets Apps Script On Edit Remove Editors From Protected Range Very Slow / Not Completing
[英]How to avoid a block of Apps Script codes execution if an input date is already there in a protected range in Google Sheets
我必須滿足兩個條件才能執行一段代碼:
輸入日期始終是當前日期,因此,我認為它應該只在最后一個受保護的數據范圍內進行搜索。
如果 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
不受保護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.