У вас есть поле в форме, где вы ожидаете, что ответ будет значением, но некоторые ответы являются строками. Это результат предшествования значению знаком «плюс» («+»).
Следующий ответ демонстрирует метод проверки, является ли ответ числом или строкой, и преобразование ответа в число. Это может быть использовано в триггере onFormSubmit (либо в форме, либо в электронной таблице ответов).
Код основан на операторе Javascript typeof .
function so5845243102() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "Form Responses 2";
var sheet = ss.getSheetByName(sheetname);
// Column D contains responses that should be values but are occassionally strings.
// Therefore test before processing.
// Cell D5 contains an example
var resp = sheet.getRange("D5").getValue();
// Logger.log(resp);//DEBUG
if (typeof resp === 'string') {
// this is a string
Logger.log("DEBUG: Response = "+resp+" is a string; length = "+resp.length+", the first character = "+resp.substring(0, 1));
// test if first character is a plus sign ("+")
if (resp.substring(0, 1) ==="+"){
// exclude the first character
var shortresp = resp.substring(1,+resp.length);
Logger.log("DEBUG: the shortened response = "+shortresp);
// now, convert the shortened response to a number and test it
var modrespnum =Number(shortresp);
// test whether the modified value is still a string
if (typeof modrespnum === 'string'){
// if a string, then more detailed investigation required;
Logger.log("DEBUG: Modified response = "+modrespnum+". It is a string - Investigate ");
return;
}
else{
// it is NOT a string
Logger.log("DEBUG: Modified response = "+modrespnum+". It is NOT a string");
// if not a string, test whether it is a number
if (typeof modrespnum === 'number') {
Logger.log("DEBUG: Modified response = "+modrespnum+". It is a number");
return;
}
else{
Logger.log("DEBUG: Modified response = "+modrespnum+". It is NOT a number - Investigate ");
}
return;
}
}
else{
// more investigation required
Logger.log("DEBUG: Response value = "+resp+" - needs investigation");
}
}
}