简体   繁体   English

Google表格应用程序脚本锁定特定日期的单元格

[英]Google Sheets App Script lock cells for specific days

I am trying to unlock certain range of cells on every month for the first 10 days and after that lock the range until next month.我试图在前 10 天的每个月解锁特定范围的单元格,然后锁定该范围直到下个月。 Here is what I've been working on.这是我一直在做的事情。 But this doesn't work as i expected.但这并不像我预期的那样有效。 Any help is appreciated.任何帮助表示赞赏。

function tenDaysAllowance() {
  var ss = SpreadsheetApp.getActive();
  var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Online Allowance");
  var todaysdate = Utilities.formatDate(new Date(), 'GMT+5', 'dd/MM/yyyy');
  var firstDate = new Date();
  var fd = firstDate;
    fd.setMonth(fd.getMonth());
    fd.setDate(1);
  var td = new Date();
    td.setMonth(td.getMonth());
    td.setDate(10);
  var firstDay = Utilities.formatDate(fd, 'GMT+5', 'dd/MM/yyyy');
  var afterTen = Utilities.formatDate(td, 'GMT+5', 'dd/MM/yyyy');
  Logger.log(firstDay);
  Logger.log(afterTen);

  if (todaysdate >= firstDay && todaysdate <= afterTen) {
    Logger.log("todays date is in range");
    var range = ss.getRange('B27:AF40');
    var protection = range.protect().setDescription('Sample protected range');
    var me = Session.getEffectiveUser();
    protection.addEditor(me);
    protection.removeEditors(protection.getEditors());
    if (protection.canDomainEdit()) {
      protection.setDomainEdit(false);
    }
  } else {
    Logger.log("todays date is not in range");
    var range = ss.getRange('B27:AF40');
    var allProtections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
    var matchingProtections = allProtections.filter(function(existingProtection) {
    return existingProtection.getRange().getA1Notation() == 'B27:AF40';
    });
    var protection = matchingProtections[0];
    protection.remove();
    };
};

In your google sheet use a today() to determine today's date and day() to determine value of today's date.在您的 google 工作表中,使用today()来确定今天的日期和day()来确定今天日期的值。

在此处输入图像描述

function myProtection() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Date Check");
var date=ss.getRange("B1");

//if it more the 10th of the month lock sheet, else allow others to edit
if(date > 10)
{
// Protect range A1:B10, then remove all other users from the list of editors.
var range = ss.getRange('B27:AF40');
var protection = range.protect().setDescription('Sample protected range');
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}


else{
    var allProtections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
    var matchingProtections = allProtections.filter(function(existingProtection) {
    return existingProtection.getRange().getA1Notation() == 'B27:AF40';
    });
    var protection = matchingProtections[0];
    protection.remove();
};
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM