1

I would like to automatically protect columns where a header row contains a date based on whether the date in the google sheet app script

Here is a screenshot of the sheet:sheet image.

I have tried and created a script that can work but it showing an error in getting last column please suggest if any edit needs to be done I am also attaching the screen shot of the error please check it out also

Script -

function Lock_Cells() {
  var ss= SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet 1'); 
  var lastcolum = sheet.getLastColumn();
for (var i = 3; i <= lastcolum; i++){
 var today = sheet.getRange(0,3).getValue();
  var Check_date = sheet.getRange(5,i).getValue();
  if (Check_date < today){
     var Lock_Range = sheet.getRange(5,i,50);
     var protection = sheet.getRange(Lock_Range).protect();
     var description = "colum " + i;                 
     protection.setDescription(description);
     var eds = protection.getEditors();
     protection.removeEditors(eds);
  }}}

Screenshot of error - https://i.stack.imgur.com/W4CYa.png

Thanks to you that problem is solved and now the sheet is showing the last column but now when I run the function it is showing an error and is not completing the script I have attached the screenshot of the error and the error showing is -

Error Exception: The starting row of the range is too small. Lock_Cells @ Code.gs:11

Screenshot https://i.stack.imgur.com/fDK9g.png

Rubén
  • 34,714
  • 9
  • 70
  • 166
akshat
  • 13
  • 4
  • 1
    Did you code something so far? – Waxim Corp May 10 '21 at 11:49
  • You can compare dates numerically by using the methods `valueOf()` or `getTime()`. – Cooper May 10 '21 at 14:30
  • I searched about it till now I found this relevant enough function Lock_Cells() { var sheet = SpreadsheetApp.getActive(); for (var i = 3; i <= 20; i++){var Check_Cell = "E" + i; var Temp = sheet.getRange(Check_Cell).getValue(); if (Temp == "Locked"){ var Lock_Range = "C" + (i + 2) + ":D" + "i"; var protection = sheet.getRange(Lock_Range).protect(); var description = "Row " + i; protection.setDescription(description); var eds = protection.getEditors(); protection.removeEditors(eds);}}}' – akshat May 10 '21 at 16:59
  • but need some help. 1st want to define the parameter in IF/ELSE statement that if today date less than the date in the column header the column should be protected automatically it should deploy else not. – akshat May 10 '21 at 17:14
  • Please [edit] the question to add to it all the relevant details. Comments are intended for giving feedback. – Rubén May 10 '21 at 17:22
  • Error messages should be add as text, not as image. – Rubén May 10 '21 at 19:35

1 Answers1

0

The error showed in the screenshot (Cannot read property 'getLastColumn' from null) occurs because the spreadsheet hasn't a sheet named Sheet 1 (var sheet = ss.getSheetByName('Sheet 1');). To fix this error replace 'Sheet 1' by the correct sheet name.

The code has other errors too:

var today = sheet.getRange(0,3).getValue();

getRange requires values equal or greater than 1

var Lock_Range = sheet.getRange(5,i,50);
var protection = sheet.getRange(Lock_Range).protect();

getRange can't use a Class Range object as argument. Please checkout https://developers.google.com/apps-script/reference/spreadsheet/sheet for the valid arguments for getRange.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Thanks to you that problem is solved and now the sheet is showing the last column but now when I run the function it is showing an error and is not completing the script I have attached the screenshot of the error and the error showing is - Error Exception: The starting row of the range is too small. Lock_Cells @ Code.gs:11 Screenshot https://i.stack.imgur.com/fDK9g.png – akshat May 11 '21 at 18:57
  • @akshat Errors as well code should be added as text, not as image. – Rubén May 11 '21 at 19:14
  • I am really sorry for that and it won't be repeated again error showing is - Error Exception: The starting row of the range is too small. Lock_Cells @ Code.gs:11 please suggest how I can resolve the issue – akshat May 11 '21 at 20:51
  • Thanks for your help the script is working fine but I need a little help with how to use dates in the script as I have assigned date to every column and assigned today's date to a particular cell when I am getting the value from them and comparing them in if statement that is not matching as the phrase is different between them can you tell how I can solve that issue Date format coming in today() formula in cell - Wed May 12 00:00:00 GMT+05:30 2021 Date format coming in today() formula in cell -Sun May 02 2021 00:00:00 GMT+0530 (India Standard Time) – akshat May 12 '21 at 12:00
  • I suggest you to spend some time learning the JavaScript basics, more specifically about data types and objects. In the particular case of date values in Google Sheets they are passed as JavaScript Date objects. In JavaScript the way to compare to Date objects is by using the valueOf or getTime. For details see https://stackoverflow.com/q/492994/1595451 – Rubén May 12 '21 at 20:53
  • 1
    Thanks for all your help @Ruben i completed the script yesterday and all your help was amazing thanks. – akshat May 13 '21 at 14:11