google-apps-script - 如何检查是否没有输入新数据,应用脚本在 Google 表格中不会执行任何操作
问题描述
我有一个 Google 表格,其中包含受保护的数据范围,例如 A2:F99、A100:F199 等。现在,如果新的数据行输入到 A200 及以后,它工作正常。但是,如果没有新数据输入到 A200 及以后,则 不应执行突出显示的代码块 (** )。并且当没有新的数据行时,( **)之间的代码块在 A1:F2 上的工作令人惊讶。请注意 A1:F1 不受保护。
那么在没有新数据的情况下如何编码,突出显示的代码块必须不被执行。
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());
var newRows = lastRow-firstRow;
**if(newRows > 0){
//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
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);
}
}**
}
解决方案
- 您正在检索工作表中日期出现的第一个和最后一个索引
- 如果只有一次出现 - 第一个和最后一个索引将是相同的,因此
lastRow = firstRow
和newRows = 0
- 这就是为什么
newRows > 0
不是一个好的标准 - 如果您想查询任何一个日期都不会出现 - 请检查
dates.indexOf(startDate) == -1
- 如indexOf()的文档中所述:
indexOf() 方法返回可以在数组中找到给定元素的第一个索引,如果不存在,则返回 -1。
使其适应您的情况:
//if startdate is present in column A
if(dates.indexOf(startDate) != -1){
//Sorting and removing duplicates
...
}
推荐阅读
- python - 列出我的 s3 中存在的每个存储桶中的对象
- javascript - 使用 Firebase 函数使用外部 api
- python - Python:如何设置触发函数的时间?
- python - 为什么我的 Django 注册表单没有呈现?
- azure - 带有 API 连接的天蓝色 Loganalytics
- excel - Excel 中的最早时间
- spring - 使用 gradle 构建特定的云合约
- vb.net - 如何从文档帐户获取模板列表?
- imagemagick - 使用 .bat 文件压缩文件夹中的 jpg imagemagick magick
- reactjs - 出问题了 。33.0.0 不是有效的 SDK 版本。选项是 35.0.0 , 36.0.0 , 38.0.0 ,37.0.0