如何修复错误异常:无法转换为整数

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

How to fix error Exception: Can not be converted to int

问题

在您提供的代码中,问题出现在以下行:

[weight, width, length, height, dimensions, volume] = [...data.split(';',)]

错误消息指出无法将字符串“500,120,80,250,120х80х250,0.0024”转换为整数。这是因为您尝试将包含浮点数的字符串分割并解构为整数,这会导致错误。

要解决此问题,您应该将数据分割后保留为字符串,而不是尝试将其转换为整数。您可以像这样修改代码:

[weight, width, length, height, dimensions, volume] = data.split(';');

这样,您将保留数据作为字符串数组,而不会尝试将其转换为整数。

此外,请确保在数据中没有包含无法转换为整数的值,否则可能需要在处理数据之前添加一些数据验证。

在修复了此问题后,您的代码应该能够按预期工作。

英文:

I'm trying to use the Property Service as an analogue of the vlookup function here is my code

function setUpProps() {
  console.log('Set up props');
  var documentProperties = PropertiesService.getDocumentProperties();
  documentProperties.deleteAllProperties();
  var sh = SpreadsheetApp.getActive();
  var shname = sh.getSheetByName('конфиг');
  var values = shname.getRange(2, 1, shname.getLastRow(), 7).getValues();
  props = {};
  for (i of values) {
    props[i[0]] = i.slice(1).join(";");
  }
  documentProperties.setProperties(props);
}

function getProps() {
  var props = PropertiesService.getDocumentProperties();
  Logger.log(props.getProperties());
}

function onEdi(e) {
  var props = PropertiesService.getDocumentProperties();
  var range = e.range;
  var sheet = range.getSheet()
  var row = range.getRow()
  var shName = e.source.getSheetName();
  var col = range.getColumn();
  var g = 'г';
  if (['мука', 'масла', 'жмыхи', 'десерты', 'лечебные тарвы'].includes(shName) && col == 2) {
    var arr = [];
    var itemNames = range.getValues();
    for (let name of itemNames) {
      var data = props.getProperty(name + g);
      Logger.log(name + g);
      var weight, width, length, height, dimensions, volume;
      if (data != null) {
        [weight, width, length, height, dimensions, volume] = [...data.split(';')]
        var row = [weight, width, length, height, dimensions, volume];
        Logger.log(row);
        arr = arr.concat([row]);
        Logger.log(arr);
      }
    }
    sheet.getRange(row, 21, range.getNumRows(), 6).setValues(arr);
  }
}

error getting in this place sheet.getRange(row, 21, range.getNumRows(), 6).setValues(arr);

Exception: Невозможно преобразовать "500,120,80,250,120х80х250,0.0024" в int.
at onEdi(Copy of PropertyService:43:11)

On function onEdi() I'm set a installable trigger for edit

my error

When I use similar code like this

function onEdit(e) {
    var props = PropertiesService.getDocumentProperties();
    const itemCol = 3;
    const groupCol = 4;
    var range = e.range;
    var sheet = range.getSheet()
    var row = range.getRow()
    if (range.getColumn() == 2) {
        var itemIDs = [];
        var itemGroups = [];
        var itemNames = range.getValues();
        for (let name of itemNames) {
            var data = props.getProperty(name);
            var itemID, groupName;
            if (data != null) {
                [itemID, groupName] = [...data.split(';')]
                itemIDs.push([itemID]);
                itemGroups.push([groupName]);
            } else {
                itemIDs.push(['NO DATA']);
                itemGroups.push(['NO DATA']);
            }

        }
        sheet.getRange(row, itemCol, range.getNumRows(), 1).setValues(itemIDs);
        sheet.getRange(row, groupCol, range.getNumRows(), 1).setValues(itemGroups);
    } 

 }

it works flawlessly
A simple trigger for edit is also used here

But I want to insert data in an array, tell me where is my mistake?

Gif what I need to output

output

答案1

得分: 1

你正在重新定义 row

onEdi(e) 的第3行:

var row = range.getRow()

但是在 onEdi(e) 的第23行:

var row = [weight, width, length, height, dimensions, volume];

所以在你的循环中,row 从一个整数重新定义为一个数组。

你可以将其更改为:

let row = [weight, width, length, height, dimensions, volume];

现在会有两个名为 row 的变量,一个在 onEdi(e) 函数的全局范围内,另一个在循环的局部范围内。

英文:

You are redefining row.

On line 3 of onEdi(e):

var row = range.getRow()

But on line 23 of onEdi(e):

var row = [weight, width, length, height, dimensions, volume];

So in your loop row is redefined from an integer to an array.

You could change it to

let row = [weight, width, length, height, dimensions, volume];

Now there would be two variables named row, one at a global scope for the onEdi(e) function and one at the local scope of the for loop.

huangapple
  • 本文由 发表于 2023年6月2日 11:29:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386955.html
匿名

发表评论

匿名网友

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

确定