Date compare If statement not working, see what is wrong - google-apps-script

I want to compare two dates and see what is similar but the if doesn't
work. I want to create a comparison with a cell value and a column of values.
function jobLogSchedule () {
var ss = SpreadsheetApp.getActiveSpreadsheet();/// Activate Sheet Application
var formSS = ss.getSheetByName("Form"); //Form Sheet
var dataBase = ss.getSheetByName("Data"); //Data Sheet
var viewSchedule = formSS.getRange("H2").getValue();
var firstDate = new Date(viewSchedule);
formSS.getRange("H2").setValue(firstDate);
formSS.getRange("H4").setValue(firstDate);
var lastRow = dataBase.getLastRow();
for(i=1; i<lastRow; i++){
var workingCell = dataBase.getRange(i,6,lastRow);
var dateDataBase = workingCell.getValue();
if (dateDataBase == viewSchedule) {
}
else {
}
}
}

try this:
function jobLogSchedule () {
var ss=SpreadsheetApp.getActive();
var fsh=ss.getSheetByName("Form");
var dsh=ss.getSheetByName("Data");
var lastRow=dsh.getLastRow();
var drg=dsh.getRange(1,6,lastRow,1);
var vA=drg.getValues();
var firstDate = new Date(fsh.getRange("H2").getValue());
fsh.getRange("H2").setValue(firstDate);
fsh.getRange("H4").setValue(firstDate);
for(i=0;i<vA.length;i++){
if (new Date(vA[i][0]).valueOf()==firstDate.valueOf()) {
//true
}
else {
//false
}
}
}

Related

How to allow non-sheet owners run scripts that involve protected cells

I have the script below where some cells are protected because they contain formula but I can script linked to buttons that when executed, it updates the cell values in these protected cells, this is fine if you are the sheet owner but if you are not you get a error saying 'You are editing protected cells....'
I have seen some solutions where the script has been deployed as a web app and then set so it always runs as the owner but can't get this working for my use case, I deployed and set as to always run as me but this only seems like half the solution?
My code is below:
//
// Save Data
function submitData() {
var SPREADSHEET_NAME = "Data";
var SEARCH_COL_IDX = 0;
var RETURN_COL_IDX = 0;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Tool"); //Form Sheet
var datasheet = ss.getSheetByName("Data"); //Data Sheet
var str = formSS.getRange("A10").getValue();
var values = ss.getSheetByName(SPREADSHEET_NAME).getDataRange().getValues();
for (var i = 0; i < values.length; i++) {
var row = values[i];
if (row[SEARCH_COL_IDX] != str ) {
//SpreadsheetApp.getUi().alert(' "Dmp #' + formSS.getRange("A4").getValue() + ' "');
// return row[RETURN_COL_IDX];
//} else {
//Input Values
var values1 = [[formSS.getRange("A10").getValue(),
formSS.getRange("B10").getValue(),
formSS.getRange("C10").getValue(),
formSS.getRange("D10").getValue(),
formSS.getRange("E10").getValue(),
formSS.getRange("F10").getValue(),
formSS.getRange("G10").getValue(),
formSS.getRange("H10").getValue(),
formSS.getRange("I10").getValue(),
formSS.getRange("J10").getValue(),
formSS.getRange("K10").getValue()]];
var values2 = [[formSS.getRange("A10").getValue(),
formSS.getRange("B10").getValue(),
formSS.getRange("C10").getValue(),
formSS.getRange("D10").getValue(),
formSS.getRange("E10").getValue(),
formSS.getRange("F10").getValue(),
formSS.getRange("G10").getValue(),
formSS.getRange("I10").getValue(),
formSS.getRange("J10").getValue(),
formSS.getRange("K10").getValue()]];
values2[0].forEach(function(val) {
if (val === "") {
throw new Error("Please fill in Project, Category, Subsystem, Description and Created By Fields.");
}
})
// Save New Data
datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 11).setValues(values1);
SpreadsheetApp.getUi().alert(' New Record Created ');
formSS.getRange("D10").clearContent();
formSS.getRange("E10").clearContent();
formSS.getRange("F10").clearContent();
formSS.getRange("G10").clearContent();
formSS.getRange("H10").clearContent();
formSS.getRange("I10").clearContent();
formSS.getRange("J10").setValue(new Date())
return row[RETURN_COL_IDX];
}
}
}
//=========================================================
// Clear form
function clearCell() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Tool"); //Form Sheet
formSS.getRange("D10").clearContent();
formSS.getRange("E10").clearContent();
formSS.getRange("F10").clearContent();
formSS.getRange("G10").clearContent();
formSS.getRange("I10").clearContent();
formSS.getRange("J10").setValue(new Date())
return true ;
}
//=====================================================================
var SPREADSHEET_NAME = "Data";
var SEARCH_COL_IDX = 0;
var RETURN_COL_IDX = 0;
function searchStr() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Tool"); //Form Sheet
var str = formSS.getRange("F4").getValue();
var values = ss.getSheetByName(SPREADSHEET_NAME).getDataRange().getValues();
for (var i = 0; i < values.length; i++) {
var row = values[i];
if (row[SEARCH_COL_IDX] == str) {
formSS.getRange("A6").setValue(row[0]) ;
formSS.getRange("B6").setValue(row[1]);
formSS.getRange("C6").setValue(row[2]);
formSS.getRange("D6").setValue(row[3]);
formSS.getRange("E6").setValue(row[4]);
formSS.getRange("F6").setValue(row[5]);
formSS.getRange("G6").setValue(row[6]);
formSS.getRange("H6").setValue(row[7]);
formSS.getRange("I6").setValue(row[8]);
formSS.getRange("J6").setValue(row[9]);
return row[RETURN_COL_IDX];
}
}
}
//===================================================================
function rowDelete() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Tool"); //Form Sheet
var datasheet = ss.getSheetByName("Data"); //Data Sheet
var ui = SpreadsheetApp.getUi();
var response = ui.alert(
'Are you sure you want to delete this record?',
ui.ButtonSet.YES_NO);
// Process the user's response.
if (response == ui.Button.YES) {
var str = formSS.getRange("F4").getValue();
var values = ss.getSheetByName(SPREADSHEET_NAME).getDataRange().getValues();
for (var i = 0; i < values.length; i++) {
var row = values[i];
if (row[SEARCH_COL_IDX] == str) {
var INT_R = i+1
datasheet.deleteRow(INT_R) ;
formSS.getRange("A6").clearContent();
formSS.getRange("B6").clearContent();
formSS.getRange("C6").clearContent();
formSS.getRange("D6").clearContent();
formSS.getRange("E6").clearContent();
formSS.getRange("F6").clearContent();
formSS.getRange("G6").clearContent();
formSS.getRange("H6").clearContent();
formSS.getRange("I6").clearContent();
formSS.getRange("J6").clearContent();
return row[RETURN_COL_IDX];
}
}
}
}
//====================================================================
function updateData() {
var SPREADSHEET_NAME = "Data";
var SEARCH_COL_IDX = 0;
var RETURN_COL_IDX = 0;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Tool"); //Form Sheet
var datasheet = ss.getSheetByName("Data"); //Data Sheet
var str = formSS.getRange("A6").getValue();
var values = ss.getSheetByName(SPREADSHEET_NAME).getDataRange().getValues();
for (var i = 0; i < values.length; i++) {
var row = values[i];
if (row[SEARCH_COL_IDX] == str) {
var INT_R = i+1
formSS.getRange("J6").setValue(new Date())
var values1 = [[formSS.getRange("A6").getValue(),
formSS.getRange("B6").getValue(),
formSS.getRange("C6").getValue(),
formSS.getRange("D6").getValue(),
formSS.getRange("E6").getValue(),
formSS.getRange("F6").getValue(),
formSS.getRange("G6").getValue(),
formSS.getRange("H6").getValue(),
formSS.getRange("I6").getValue(),
formSS.getRange("J6").getValue()]];
var values2 = [[formSS.getRange("A6").getValue(),
formSS.getRange("B6").getValue(),
formSS.getRange("C6").getValue(),
formSS.getRange("D6").getValue(),
formSS.getRange("E6").getValue(),
formSS.getRange("F6").getValue(),
formSS.getRange("G6").getValue(),
formSS.getRange("I6").getValue(),
formSS.getRange("J6").getValue()]];
values2[0].forEach(function(val) {
if (val === "") {
throw new Error("Please fill in Revisions, Project, Category, Subsystem, Description and Updated By Fields.");
}
})
datasheet.getRange(INT_R, 1, 1, 10).setValues(values1);
formSS.getRange("A6").clearContent();
formSS.getRange("B6").clearContent();
formSS.getRange("C6").clearContent();
formSS.getRange("D6").clearContent();
formSS.getRange("E6").clearContent();
formSS.getRange("F6").clearContent();
formSS.getRange("G6").clearContent();
formSS.getRange("H6").clearContent();
formSS.getRange("I6").clearContent();
formSS.getRange("J6").clearContent();
formSS.getRange("E4").clearContent();
SpreadsheetApp.getUi().alert(' Record Updated ');
return row[RETURN_COL_IDX];
}
}
}
There are several posts about this, I'll paste a response from one from yesterday. What I recommend specifically in your case is to run the script when there's an edit bye the user in a certain cell. For example a Tickbox, or a Drop-down menu (in a cell) that allows the user to select which function to run:
If you already have an onEdit function working, that's a simple trigger run by whoever is editing the sheet. Meaning that if you protect column A, it won't be editable by that simple trigger because the user won't have permissions
In order to work this out, I encourage you to protect your column as explained here, change your name function or extract in a new function the part about this specific code you're talking about; and set an installable trigger that runs on event. This way it'll be run as you used to but as it came from your own account. As you have permissions for editing ColA the timestamp will be set by the installable trigger but the other user won't be able to edit it since he/she doesn't have the permissions. Try it and let me know!

issue during google sheet macro

I have an issue with this google sheet macro. Error message is: Exception: Range not found
I am trying to perform this script in order to copy recipient string (that can change according to data in P2 cell.
Can you help me?
Thanks
function macro()
{
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange("A2:L1000");
var data = dataRange.getValues();
for (i in data)
{
var rowData = data[i];
var recipient = rowData[0];
var parameter1 = rowData[1];
if (parameter1 == "OK")
{
sheet.getRange('P2').activate();
sheet.getRange(recipient).copyTo(sheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
}
}
}
Perhaps this is what you want:
function macro() {
var sh = SpreadsheetApp.getActiveSheet();
var rg = sh.getRange("A2:L1000");
var data = rg.getValues();
data.forEach((r,i)=>{
if(r[1] == "OK") {
sh.getRange(r[0]).copyTo(sh.getRange(i + 2, 16),SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
}
})
}
I have made something like this but it is only copy/paste first row (copy B2 to P2 works fine).
If I try to copy B3 to P2 it does not work..
function macro()
{
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange("A2:L1000");
var data = dataRange.getValues();
for (i in data)
{
var rowData = data[i];
var parameter1 = rowData[10];
if (parameter1 == "OK")
{
sheet.getRange('P2').activate();
sheet.getRange("B" +(i+2)).copyTo(sheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
}
}
}

getLastRow() affecting other cells

Hello I am currently working on a time tracking system. With the following code I track the time how long a value was in a cell. This time is recorded in another worksheet and this is done continuously by appendRow ().
function onEdit(e) {
addTimestamp(e);
}
function addTimestamp(e) {
var ui = SpreadsheetApp.getUi();
var ws = "Tabellenblatt2";
var ss = e.source;
var targetSheet = ss.getSheetByName("Tabellenblatt1");
var range = targetSheet.getRange(3, 2, 1000, 1);
var currentDate = new Date();
var scriptProperties = PropertiesService.getScriptProperties();
if (e.source.getActiveSheet().getName() === ws) {
var cell = ss.getActiveCell();
var val = cell.getValue();
var sourceRowIndex = cell.getRow();
if (val != "") {
let rowToAdd = [val, "", currentDate, ""]
targetSheet.appendRow(rowToAdd);
scriptProperties.setProperty(sourceRowIndex, targetSheet.getLastRow());
} else {
var rowIndex = Number(scriptProperties.getProperty(sourceRowIndex));
if (rowIndex) targetSheet.getRange(rowIndex, 4).setValue(currentDate);
}
}
}
Now one Picture to show my Problem:
The problem is that the cells should start in row 1, is that possible with getLastRow ()?
Determine the last row with content based on another column (e.g. column A):
function onEdit(e) {
addTimestamp(e);
}
function addTimestamp(e) {
var ui = SpreadsheetApp.getUi();
var ws = "Tabellenblatt2";
var ss = e.source;
var targetSheet = ss.getSheetByName("Tabellenblatt1");
var range = targetSheet.getRange(3, 2, 1000, 1);
var currentDate = new Date();
var scriptProperties = PropertiesService.getScriptProperties();
if (e.source.getActiveSheet().getName() === ws) {
var cell = ss.getActiveCell();
var val = cell.getValue();
var sourceRowIndex = cell.getRow();
if (val != "") {
let rowToAdd = [val, "", currentDate, ""]
let rowA=targetSheet.getRange("A1:A").getValues().filter(String).length+1; // new code
targetSheet.getRange(rowA,1,1,rowToAdd.length).setValues([rowToAdd]); // new code
scriptProperties.setProperty(sourceRowIndex, targetSheet.getLastRow());
} else {
var rowIndex = Number(scriptProperties.getProperty(sourceRowIndex));
if (rowIndex) targetSheet.getRange(rowIndex, 4).setValue(currentDate);
}
}
}
it would be easier to change the formula for Time to not display anything at all unless there was an entry, then getLastRow() will work just fine. For help with that, share the formula that's calculating the time.

Copy rows that meet a date in Google Sheets

I want to be able to copy rows from a source sheet to a destination sheet that is the date in a range equals a target date.
I am using the following script and works perfectly when the target is a 'text', but as soon as I try to use a 'date' I get the following error: TypeError: Cannot read property "length" from undefined.
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Source'); //source sheet
var testrange = sheet.getRange('F:F');
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName('Destination'); //destination sheet
var data = [];
var j =[];
//Condition check in L:L; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == 'target') {
data.push.apply(data,sheet.getRange(i+1,1,1,45).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
Thanks
Simon
I wasn't sure what your target was so I assumed current date and proceeded anyway. If it's wrong let me know.
function copyrange() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Source');
var vA=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
var csh=ss.getSheetByName('Destination');
var found=false;
var dt=new Date();
var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight
var output=[];
for (var i=0;i<vA.length;i++) {
var date=new Date(vA[i][4]);//can be a string the constructor recognizes or a date
var dv=new Date(date.getFullYear(),date.getMonth(),date.getDate()).valueOf();//midnight
if (dv==today) {
output.push(vA[i]);
var found=true;
}
}
if(found) {
csh.getRange(csh.getLastRow()+1,1,output.length,output[0].length).setValues(output);
}else{
SpreadsheetApp.getUi().alert('No matches');
}
}
The following function will get those rows that have todays date and this email email1#test.com.
function copyrange(target) {
var target=target||'email1#test.com';
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Source');
var vA=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
var csh=ss.getSheetByName('Destination');
var found=false;
var dt=new Date();
var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight
var output=[];
for (var i=0;i<vA.length;i++) {
var date=new Date(vA[i][4]);//can be a string the constructor recognizes or a date
var dv=new Date(date.getFullYear(),date.getMonth(),date.getDate()).valueOf();//midnight
if (dv==today && vA[i][6]==target) {
output.push(vA[i]);
var found=true;
}
}
if(found) {
csh.getRange(csh.getLastRow()+1,1,output.length,output[0].length).setValues(output);
}else{
SpreadsheetApp.getUi().alert('No matches');
}
}
And you can change the target email by passing an email to the function.
This function covers dates that have occurred in the last week:
function copyrange(target) {
var target=target||'email1#test.com';
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Source');
var vA=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
var csh=ss.getSheetByName('Destination');
var found=false;
var dt=new Date();
var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight
var sevendaysago=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-7).valueOf();//midnight
var output=[];
for (var i=0;i<vA.length;i++) {
var date=new Date(vA[i][4]);//can be a string the constructor recognizes or a date
var dv=new Date(date.getFullYear(),date.getMonth(),date.getDate()).valueOf();//midnight
if (dv>=sevendaysago && dv<=today && vA[i][6]==target) {
output.push(vA[i]);
var found=true;
}
}
if(found) {
csh.getRange(csh.getLastRow()+1,1,output.length,output[0].length).setValues(output);
}else{
SpreadsheetApp.getUi().alert('No matches');
}
}
This function only copies B and G of the matching row.
function copyrange(target) {
var target=target||'email1#test.com';
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Source');
var vA=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
var csh=ss.getSheetByName('Destination');
var found=false;
var dt=new Date();
var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight
var sevendaysago=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-7).valueOf();//midnight
var output=[];
for (var i=0;i<vA.length;i++) {
var date=new Date(vA[i][4]);//can be a string the constructor recognizes or a date
var dv=new Date(date.getFullYear(),date.getMonth(),date.getDate()).valueOf();//midnight
if (dv>=sevendaysago && dv<=today && vA[i][6]==target) {
output.push([vA[i][1],vA[6]]);
var found=true;
}
}
if(found) {
csh.getRange(csh.getLastRow()+1,1,output.length,output[0].length).setValues(output);
}else{
SpreadsheetApp.getUi().alert('No matches');
}
}

How to get the cell position of a button or rather how to get a variable for the execution of a button?

With the function I woud like to do the following:
Flow
1.) Get the current User ID
2.) Finding the user's lines where the states are on the alert icon
3.) Change the date in column K to the current date using the format "dd.MM.yyyy"
4.) Generate a time stamp in column L using the format "HH:mm:ss"
5.) Generate a time stamp in the column P using the format "dd.MM.yyyy' 'HH:mm"
Therefore I used the code below.
Unfortunately the code doesn't work. I did not got any error.
What am I doing wrong?
function CheckAll(){
var userID=getUserId();
if(userID) {
var timezone = "GMT+2";
var TimestampFormat1 = "dd.MM.yyyy' 'HH:mm";
var TimestampFormat2 = "HH:mm:ss";
var ss=SpreadsheetApp.getActiveSpreadsheet();
var tickerCheck=ss.getSheetByName('WATCHLIST-Pflege');
var State=tickerCheck.getRange("O1:O2").getCell(2,1).getValues();
var startRow=16;
var lastRow=tickerCheck.getLastRow()-startRow+1;
var rngStateCheck=tickerCheck.getRange(startRow,15,lastRow,1);
var StateCheck=rngStateCheck.getValues();
var userIds=tickerCheck.getRange(startRow,1,lastRow,1).getValues();
var rngLastCheck=tickerCheck.getRange(startRow,11,lastRow,1);
var rngTimeStamp1=tickerCheck.getRange(startRow,12,lastRow,1);
var rngTimeStamp2=tickerCheck.getRange(startRow,16,lastRow,1);
var date=new Date();
for(var i=0;i<StateCheck.length;i++){
if(StateCheck[i][14]==State && userIds[i][0]==userID){
rngLastCheck.getCell(i+1, 1).setValue(date);
rngTimeStamp1.getCell(i+1, 1).setValue(Utilities.formatDate(date, timezone, TimestampFormat2));
rngTimeStamp2.getCell(i+1, 1).setValue(Utilities.formatDate(date, timezone, TimestampFormat1));
}
}
}else{
SpreadsheetApp.getUi().alert('Falsche User ID');
}
}
function getUserId() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Startseite');
var rg=sh.getRange(35,3,sh.getLastRow()-3,3);
var vA=rg.getValues();
var userEmail=Session.getEffectiveUser().getEmail();
for(var i=0;i<vA.length;i++) {
if(vA[i][0]==userEmail) {
return vA[i][1];
break;
}
}
return null;
}
I solved the problem by adding the variable var StateToCheck=StateCheck[i][0] to the for loop.
Now the code looks like this:
function CheckAll(){
var userID=getUserId();
if(userID) {
var timezone = "GMT+2";
var TimestampFormat1 = "dd.MM.yyyy' 'HH:mm";
var TimestampFormat2 = "HH:mm:ss";
var ss=SpreadsheetApp.getActiveSpreadsheet();
var tickerCheck=ss.getSheetByName('WATCHLIST-Pflege');
var State=tickerCheck.getRange("O1:O2").getCell(2,1).getValues();
var startRow=16;
var lastRow=tickerCheck.getLastRow()-startRow+1;
var rngStateCheck=tickerCheck.getRange(startRow,15,lastRow,1);
var StateCheck=rngStateCheck.getValues();
var userIds=tickerCheck.getRange(startRow,1,lastRow,1).getValues();
var rngLastCheck=tickerCheck.getRange(startRow,11,lastRow,1);
var rngTimeStamp1=tickerCheck.getRange(startRow,12,lastRow,1);
var rngTimeStamp2=tickerCheck.getRange(startRow,16,lastRow,1);
var date=new Date();
for(var i=0;i<StateCheck.length;i++){
var StateToCheck=StateCheck[i][0]
if((StateToCheck==State)&&(userIds[i][0]==userID)){
rngLastCheck.getCell(i+1, 1).setValue(date);
rngTimeStamp1.getCell(i+1, 1).setValue(Utilities.formatDate(date, timezone, TimestampFormat2));
rngTimeStamp2.getCell(i+1, 1).setValue(Utilities.formatDate(date, timezone, TimestampFormat1));
}
}
}else{
SpreadsheetApp.getUi().alert('Falsche User ID');
}
}
function getUserId() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Startseite');
var rg=sh.getRange(35,3,sh.getLastRow()-3,3);
var vA=rg.getValues();
var userEmail=Session.getEffectiveUser().getEmail();
for(var i=0;i<vA.length;i++) {
if(vA[i][0]==userEmail) {
return vA[i][1];
break;
}
}
return null;
}
You could use this approach:
function CheckAll1(){
var userID=getUserId();
if(userID) {
var ss=SpreadsheetApp.getActive();
var tsh=ss.getSheetByName('Tickerprüfung');
var startRow=12;
var lastRow=tsh.getLastRow()-startRow+1;
var range=tsh.getRange(startRow,12,tsh.getLastRow()-startRow+1,1);
var tvA=range.getValues();
var userIds=tsh.getRange(startRow,1,tsh.getLastRow()-startRow+1,1).getValues();
var lcrg=tsh.getRange(startRow,9,tsh.getLastRow()-startRow+1,1);
var tsrg=tsh.getRange(startRow,10,tsh.getLastRow()-startRow+1,1);
var today=new Date();
var dateInMs=new Date(today.getFullYear(), today.getMonth(), today.getDate()).getTime();
for(var i=0;i<tvA.length;i++){
var nextDate=new Date(tvA[i][0]);
if(Object.prototype.toString.call(nextDate) === '[object Date]' && userIds[i][0]==userID) {
if(dateInMs>=nextDate.getTime()){
lcrg.getCell(i+startRow, 1).setValue(today);
tsrg.getCell(i+startRow, 1).setValue(new Date().toLocaleTimeString());
}
}
}
}else{
SpreadsheetApp.getUi().alert('Invalid UserId');
}
}
function getUserId() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Startseite');
var rg=sh.getRange(20,2,sh.getLastRow()-19,2);
var vA=rg.getValues();
var userEmail=Session.getEffectiveUser().getEmail();
for(var i=0;i<vA.length;i++) {
if(vA[i][0]==userEmail) {
return vA[i][1];
break;
}
}
return null;
}
And then in the Startseite sheet you could associate user emails with UserId's in this way.
Look closely at the code as I have corrected some errors pertaining to the calculation of rows that did not account for startRow=12 also third parameter in a range is the number of rows not the last row.