英文:
How do I convert string cell value of a number into a number increment by 1?
问题
I am trying to fetch the last row's row id (located in the first column) to increment it by one once there is a new row added. However, I am not sure on how to convert the string into a number to increment by 1.
What I tried:
var url = "";
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("Data");
var rowID = ws.getRange(ws.getLastRow(), 1).getValues().toString().parseInt()+1;
console.log(rowID);
Which is not a function based on the log.
英文:
I am trying to fetch the last row's row id (located in the first column) to increment it by one once there is a new row added. However I am not sure on how to convert the string into a number to increment by 1
What I tried:
var url = "";
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("Data");
var rowID = ws.getRange(ws.getLastRow(), 1).getValues().toString().parseInt()+1;
console.log(rowID);
Which is not a function based on the log.
答案1
得分: 2
From:
var rowID = ws.getRange(ws.getLastRow(), 1).getValues().toString().parseInt()+1;
To:
var rowID = (parseInt(ws.getRange(ws.getLastRow(), 1).getValue(), 10) || 0) + 1;
or
var rowID = (Number(ws.getRange(ws.getLastRow(), 1).getValue()) || 0) + 1;
or, in the event the value in the last row is known to always be a number or a blank, the following modification might be able to be used.
var rowID = ws.getRange(ws.getLastRow(), 1).getValue() + 1;
Reference:
英文:
If the values of your id
is 1
, 2
, 3
,,, , how about the following modification?
From:
var rowID = ws.getRange(ws.getLastRow(), 1).getValues().toString().parseInt()+1;
To:
var rowID = (parseInt(ws.getRange(ws.getLastRow(), 1).getValue(), 10) || 0) + 1;
or
var rowID = (Number(ws.getRange(ws.getLastRow(), 1).getValue()) || 0) + 1;
or, in the event the value in the last row is known to always be a number or a blank, the following modification might be able to be used.
var rowID = ws.getRange(ws.getLastRow(), 1).getValue() + 1;
Reference:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论