Invalid argument: latitude. Should be of type: number (file Code.gs, line 22) - Googel Ads Script
I am currently trying to import latitude and longitude values from a sheet, into my Google ads campaign through a script that looks like this:
function main() { var SPREADSHEET_URL = 'https://docs.google.com/spreadsheets/d/17w74flZ3AD7r7wIbAoYYkffUuJfxGB0-a9lhjBStzW4/edit#gid=0'; var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL); var sheet = spreadsheet.getActiveSheet(); var data = sheet.getRange("A:E").getValues(); for (i in data) { if (i == 0) { continue; } var [CampaignName, latitude, longitude, radius, unit] = data[i]; if (CampaignName == "") { break; } else { var campaignIterator = AdWordsApp.campaigns() .withCondition("CampaignName CONTAINS_IGNORE_CASE '" + CampaignName +"'") .get(); while (campaignIterator.hasNext()) { var campaign = campaignIterator.next(); campaign.addProximity(latitude, longitude, radius, unit); } } } }
However, when running the script, I keep getting the error "Invalid argument: latitude. Should be of type: number (file Code.gs, line 22)" What am I doing wrong? (also the sheet link is open for anyone, and its a back up so no worries).
Filter and format the data properly, with this should be enough:
function main() { var SPREADSHEET_URL = 'https://docs.google.com/spreadsheets/d/17w74flZ3AD7r7wIbAoYYkffUuJfxGB0-a9lhjBStzW4/edit#gid=0'; var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL); var sheet = spreadsheet.getActiveSheet(); //Note you get only relevant data ranges var range = sheet.getDataRange(); var data = range.getValues(); //Now we got only non blank cells for (i in data) { if (i == 0) { continue; } var [CampaignName, latitude, longitude, radius, unit] = data[i]; // Parse strings to float latitude = parseFloat(latitude); longitude = parseFloat(longitude); radius = parseFloat(radius); // End of data conversion if (CampaignName == "") { break; } else { var campaignIterator = AdWordsApp.campaigns() .withCondition("CampaignName CONTAINS_IGNORE_CASE '" + CampaignName +"'") .get(); while (campaignIterator.hasNext()) { var campaign = campaignIterator.next(); campaign.addProximity(latitude, longitude, radius, unit); } } } }I've added two things:
This getDataRange() function gets only non trivial data, so you are not getting a lot of blank rows.I've kept your if(CampaignName == "") condition to avoid errors in case you have a blank row in between your data rows.
Second one is parseFloat, it converts your string to numbers and solves your initial problem.As a tip:
Formating your spreadsheet numeric cells as numbers help on the importation process, because you'll import them as such, and you won't need to convert it.