英文:
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: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.
英文:
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());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论