Google Sheet Script should increment the value of the column 1 by 1, but it is concatenating 1. I am not great at google Scripts. Could you help me find what am i Doing wrong.
function onFormSubmit(e) {
var columnNumber = 1; // first col is 0
var initialId = 18;
var sp = PropertiesService.getScriptProperties();
var id = sp.getProperty("req_id") || initialId ;
id = id.split(".")[0] ;
e.range.offset(0, columnNumber, 1, 1).setValue(id);
sp.setProperty("req_id", id + 1);
}
The output is looking like this in the sheet
1811
18111
181111
18111111
1
every run.If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
getProperty()
is the string type. In this case, even when the number is put with setProperty()
, when the value is retrieved with getProperty()
, the retrieved value is the string type.split()
, an error occurs. Because split()
is used for the string.When your script is modified, it becomes as follows.
function onFormSubmit(e) {
var columnNumber = 1; // first col is 0
var initialId = 18;
var sp = PropertiesService.getScriptProperties();
var id = Number(sp.getProperty("req_id")) || initialId; // Modified
e.range.offset(0, columnNumber, 1, 1).setValue(id);
sp.setProperty("req_id", id + 1);
}
If I misunderstood your question and this was not the result you want, I apologize.