将字符串单元格中的数字值转换为增加1的数字如何操作?

huangapple go评论57阅读模式
英文:

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:

huangapple
  • 本文由 发表于 2023年2月8日 13:44:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定