硬编码数字与提示框答案的差异。

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

Difference answer from hard coded number and prompt box answer

问题

I have used indexOf() with no problems when getting data from cells. This is the first time from a prompt.

function clearRecentItem() {
  var ui = SpreadsheetApp.getUi();
  var result = ui.prompt('What ID number do you wish to delete?', ui.ButtonSet.OK_CANCEL);
  //var idToFind = result.getResponseText();
  var idToFind = 4001;
  console.log("idtoFind:  " + idToFind);
  var flatAssetIds = rentalAssets.map(r => r[5]);
  var indexOfAsset = flatAssetIds.indexOf(idToFind);
  console.log(flatAssetIds);
  console.log(indexOfAsset);
  rental.getRange("B1").setValue(indexOfAsset);
  return; //actually more code follows but not relevant
}

When run with hard coded 4001 as var idToFind I get the index number:

Apr 16, 2023, 3:22:03PM	Debug	idtoFind:  4001
Apr 16, 2023, 3:22:03PM	Debug	[ 'ID', 2003, 4001, 8003, '', '', '', '', '', '', '' ]
Apr 16, 2023, 3:22:03PM	Debug	indexOfAsset: 2

But when I get the answer from a Prompt box by moving the // down a line, I get -1 for not found:

Apr 16, 2023, 3:25:10PM	Debug	idtoFind:  4001
Apr 16, 2023, 3:25:10PM	Debug	[ 'ID', 2003, 4001, 8003, '', '', '', '', '', '', '' ]
Apr 16, 2023, 3:25:10PM	Debug	indexOfAsset: -1

It appears that the idtoFind is identical and the value is present in the array so I am not sure how to troubleshoot further.
P.S. the sheet is globally declared earlier in the script and the button is on the sheet.

英文:

I have used indexOf() with no problems when getting data from cells. This is the first time from a prompt.

function clearRecentItem() {
  var ui = SpreadsheetApp.getUi();
  var result = ui.prompt('What ID number do you wish to delete?', ui.ButtonSet.OK_CANCEL);
  //var idToFind = result.getResponseText();
  var idToFind = 4001;
  console.log("idtoFind:  "+idToFind);
  var flatAssetIds = rentalAssets.map(r => r[5]);
  var indexOfAsset = flatAssetIds.indexOf(idToFind);
  console.log(flatAssetIds);
  console.log(indexOfAsset);
  rental.getRange("B1").setValue(indexOfAsset);
  return; //actually more code follows but not relevant

When run with hard coded 4001 as var idToFind I get the index number:

Apr 16, 2023, 3:22:03 PM	Debug	idtoFind:  4001
Apr 16, 2023, 3:22:03 PM	Debug	[ 'ID', 2003, 4001, 8003, '', '', '', '', '', '', '' ]
Apr 16, 2023, 3:22:03 PM	Debug	indexOfAsset: 2

But when I get the answer from a Prompt box by moving the // down a line, I get -1 for not found:

Apr 16, 2023, 3:25:10 PM	Debug	idtoFind:  4001
Apr 16, 2023, 3:25:10 PM	Debug	[ 'ID', 2003, 4001, 8003, '', '', '', '', '', '', '' ]
Apr 16, 2023, 3:25:10 PM	Debug	indexOfAsset: -1

It appears that the idtoFind is identical and the value is present in the array so I am not sure how to troubleshoot further.
P.S. the sheet is globally declared earlier in the script and the button is on the sheet.

答案1

得分: 1

The prompt response is a string you need to change to a number. If the value passed to parseInt is not a number the results will be NaN (Not a Number).

Change

var idToFind = parseInt(result.getResponseText());
英文:

The prompt response is a string you need to change to a number. If the value passed to parseInt is not a number the results will be NaN (Not a Number).

Change

var idToFind = result.getResponseText();

To

var idToFind = parseInt(result.getResponseText());

huangapple
  • 本文由 发表于 2023年4月17日 03:46:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029992.html
匿名

发表评论

匿名网友

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

确定