问题
I coded something for google apps script. It is to increment ID +1 based on the last row. Everything is working so far except for the numbering of the new ID, instead of appearing as a number.
The result appears as R-NaN instead of R-002 or something similar
What do you think should I revise in my code? Thank you.
function new_item() {
// Get current spreadsheet
var app = SpreadsheetApp;
var ss = app.getActiveSpreadsheet();
var mysheet = ss.getActiveSheet();
// Set date to today in update field at the top of the sheet
var now = new Date();
mysheet.getRange(1,4).setValue(now);
// Last non-empty row
var rlast = mysheet.getLastRow();
Logger.log("Last row = " + rlast);
// Insert Row below
mysheet.insertRows(rlast+1);
var r = rlast+1;
// Copy format from row above
var sourcerange = mysheet.getRange(rlast + ":" + rlast);
var targetrange = mysheet.getRange(r + ":" + r);
sourcerange.copyTo(targetrange, {formatOnly:true});
// Col. 2 : Risk identity
var riskid = mysheet.getRange(rlast,2).getValue();
if (riskid.length > 3){
// Extract number ex. 3
var riskidnb = riskid.substring(1,riskid.length);
// Increase risk number +1
riskidnb++
// Convert to string "0004"
var s = "000" + riskidnb
// Write risk nb i.e. "R004"
mysheet.getRange(r,2).setValue("R-"+ s.substring(s.length-4))
}
``´
回答1:
Explanation / Issue:
Your code really depends on the value of the cell in column B last row:
var riskid = mysheet.getRange(rlast,2).getValue();
There are two scenarios but I believe the second applies to your issue:
If the value in the cell is a number (e.g. 35233) then
riskidwill be an integer and thereforeriskid.lengthwill returnnulland as a result theifcondition will evaluate tofalse. In this case, you can either usegetDisplayValueortoString()instead to get the number as string and then you can apply.lengthto it:var riskid = mysheet.getRange(rlast,2).getValue();If the value in the cell is a string (e.g.
R112) then theifcondition will evaluate totrue. If you do that:var riskidnb = riskid.substring(1,riskid.length);riskidnbwill be112but this is still a string and therefore if you doriskidnb++you will getNANlike the issue you have right now. In order to fix that, convertriskidnbto integer:var riskidnb = parseInt(riskid.substring(1,riskid.length));then you can do
riskidnb++and finally convert it back to string:var s = "000" + riskidnb.toString();
Solution:
function new_item() {
// Get current spreadsheet
var app = SpreadsheetApp;
var ss = app.getActiveSpreadsheet();
var mysheet = ss.getActiveSheet();
// Set date to today in update field at the top of the sheet
var now = new Date();
mysheet.getRange(1,4).setValue(now);
// Last non-empty row
var rlast = mysheet.getLastRow();
Logger.log("Last row = " + rlast);
// Insert Row below
mysheet.insertRows(rlast+1);
var r = rlast+1;
// Copy format from row above
var sourcerange = mysheet.getRange(rlast + ":" + rlast);
var targetrange = mysheet.getRange(r + ":" + r);
sourcerange.copyTo(targetrange, {formatOnly:true});
// Col. 2 : Risk identity
var riskid = mysheet.getRange(rlast,2).getValue();
if (riskid.length > 3){
// Extract number ex. 3
var riskidnb = parseInt(riskid.substring(1,riskid.length));
// Increase risk number +1
riskidnb++
// Convert to string "0004"
var s = "000" + riskidnb.toString();
// Write risk nb i.e. "R004"
mysheet.getRange(r,2).setValue("R-"+ s.substring(s.length-4))
}
}
Output:
来源:https://stackoverflow.com/questions/64680059/increment-id-from-last-row-google-apps-script