Reputation: 71
somehow .getRange not working ?? works this way in every other script why not here ? Log is attached...
Error is at line
**
"var days srcSheet.getRange("F" + i);"
**
function Test() {
var sSheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = sSheet.getSheetByName("Refinery");
var lastRow = srcSheet.getLastRow();
for(var i = 0; i <= lastRow; i++) {
var days = srcSheet.getRange("F" + i);
var day = days.getValue();
var hours = srcSheet.getRange("G" + i)
var hour = hours.getValue();
var cells = srcSheet.getRange("E" + i);
if((day != "")||(day != null)&&(hour != "")||(hour != null)&&(cells == "-"))
{
//Datum berechnen
var neudate = new Date()
var Btag = 1000*60*60*24*day
var Bstd = 1000*60*60*hour
var DATUM = (new Date(neudate.getTime()+ Btag+Bstd) )
DATUM = Utilities.formatDate(DATUM, "GMT+2", "dd.MM.yyyy HH:mm:ss")
cells.setValue(DATUM);
}
}
}
Upvotes: 1
Views: 658
Reputation: 201378
getRange(a1Notation)
, the start number of A1Notation is 1
. But in your script, the 1st number is 0
. I think that this is the reason of your issue.getValue
and setValue
are used in a loop. In this case, the process cost of the script becomes high. RefIn this modification, your script is modified. In this case, please modify your script as follows.
for(var i = 0; i <= lastRow; i++) {
for(var i = 1; i <= lastRow; i++) {
In this modification, your script is modified by reducing the process cost. In this case, please modify your script as follows.
function Test() {
var sSheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = sSheet.getSheetByName("Refinery");
var lastRow = srcSheet.getLastRow();
var range = srcSheet.getRange("E1:G" + lastRow);
var values = range.getValues().map(([cells, day, hour]) => {
if ((day != "") || (day != null) && (hour != "") || (hour != null) && (cells == "-")) {
var neudate = new Date();
var Btag = 1000 * 60 * 60 * 24 * day
var Bstd = 1000 * 60 * 60 * hour
var DATUM = (new Date(neudate.getTime() + Btag + Bstd))
DATUM = Utilities.formatDate(DATUM, "GMT+2", "dd.MM.yyyy HH:mm:ss")
return [DATUM];
}
return [cells]
});
range.offset(0, 0, values.length, 1).setValues(values);
}
Upvotes: 1