4

This is my first post. I tried searching via Google and this site but really couldn't find any info about typecasting or converting numerical values within GAS specifically in this scenario. So here the problem:

I have a very basic script attached to a Google Spreadsheet. The script simply keeps track of the last row that was populated, increments the value by one, so that the next row can be populated. I'm storing the "last row used" as a Project Property. For some reason, the script keeps writing the value back into Project Properties as a decimal value, which throws off the whole script since it seems I can't use a decimal value to reference a cell location.

Here is the code snippet:

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var value = ss.getSheetByName("Sheet1").getRange("D13").getValue().toString();
  var last = ScriptProperties.getProperty("last");

  var therowx = parseInt(ScriptProperties.getProperty("therow"));
  Browser.msgBox("The last row is: " + therowx);

  if( value != last ) {
    ScriptProperties.setProperty("last", value);
    ScriptProperties.setProperty("therow", therowx + 1);
  }
}

Assuming that I've pre-set the Property to 1 before running the script, once this method fires, it sets it to 2.0 instead of just 2. I've tried wrapping the method call with the Number() and parseInt() functions but they don't seem to make a difference.

I'm open to suggestions, as I'm sure I'm doing something wrong but just not quite sure what.

4

1 回答 1

0

您似乎正在使用电子表格中的值 var value = ss.getSheetByName("Sheet1").getRange("D13").getValue().toString();
这是您获得小数点的情况,因为 Google 电子表格将默认为小数点后一位。您需要突出显示列或单元格并将格式更改为 Number>Rounded 以摆脱它。
我确定我在这里遗漏了您完整脚本的一些组件,但是您是否尝试过“getLastRow”功能?教程中有一些使用它的脚本,它是一种经过验证的真实方法,可以找到工作表的最后一行。

于 2012-10-24T02:25:48.413 回答