I've got my key from as required and have created the script similar to how it appears in the article. I have made an ad campaign that is currently paused, with a few ads in it, half with spelling errors to test if the script works.
I preview the script and it comes back with no logger output, and no data is getting pulled into my Google Drive which should be occurring as part of the script.
I'm thinking that perhaps I'm not customising the script enough. Can you please look it over and give me an idea of what I would need to adjust? I have removed my key here, but it is usually inserted on line 5. It's probably something quite simple that I'm doing wrong, but I have zero experiences with scripts and nobody to help. Thanks!
function main() {
function BingSpellChecker(config) {
this.BASE_URL = '
https://api.cognitive.microsoft.com/bing/v5.0/spellcheck';
this.CACHE_FILE_NAME = 'spellcheck_cache.json';
this.key = config.key;
this.toIgnore = config.toIgnore;
this.cache = null;
this.previousText = null;
this.previousResult = null;
this.delay = (config.delay) ? config.delay : 60000/7;
this.timeOfLastCall = null;
this.hitQuota = false;
// Given a set of options, this function calls the API to check the spelling
// options:
// options.text : the text to check
// options.mode : the mode to use, defaults to 'proof'
// returns a list of misspelled words, or empty list if everything is good.
this.checkSpelling = function(options) {
if(this.toIgnore) {
options.text = options.text.replace(new RegExp(this.toIgnore.join('|'),'gi'), '');
}
options.text = options.text.replace(/{.+}/gi, '');
options.text = options.text.replace(/[^a-z ]/gi, '').trim();
if(options.text.trim()) {
if(options.text == this.previousText) {
Logger.log('INFO: Using previous response.');
return this.previousResult;
}
if(this.cache) {
var words = options.text.split(/ +/);
for(var i in words) {
Logger.log('INFO: checking cache: '+words[i]);
if(this.cache.incorrect[words[i]]) {
Logger.log('INFO: Using cached response.');
return [{"offset":1,"token":words[i],"type":"cacheHit","suggestions":[]}];
}
}
}
var url = this.BASE_URL;
var config = {
method : 'POST',
headers : {
'Ocp-Apim-Subscription-Key' : this.key,
'Content-Type' : 'application/x-www-form-urlencoded'
},
payload : 'Text='+encodeURIComponent(options.text),
muteHttpExceptions : true
};
if(options && options.mode) {
url += '?mode='+options.mode;
} else {
url += '?mode=proof';
}
if(this.timeOfLastCall) {
var now = Date.now();
if(now - this.timeOfLastCall < this.delay) {
Logger.log(Utilities.formatString('INFO: Sleeping for %s milliseconds',
this.delay - (now - this.timeOfLastCall)));
Utilities.sleep(this.delay - (now - this.timeOfLastCall));
}
}
var resp = UrlFetchApp.fetch(url, config);
this.timeOfLastCall = Date.now();
if(resp.getResponseCode() != 200) {
if(resp.getResponseCode() == 403) {
this.hitQuota = true;
}
throw JSON.parse(resp.getContentText()).message;
} else {
var jsonResp = JSON.parse(resp.getContentText());
this.previousText = options.text;
this.previousResult = jsonResp.flaggedTokens;
for(var i in jsonResp.flaggedTokens) {
this.cache.incorrect[jsonResp.flaggedTokens[i].token] = true;
}
return jsonResp.flaggedTokens;
}
} else {
return [];
}
};
// Returns true if there are spelling mistakes in the text toCheck
// toCheck : the phrase to spellcheck
// returns true if there are words misspelled, false otherwise.
this.hasSpellingIssues = function(toCheck) {
var issues = this.checkSpelling({ text : toCheck });
return (issues.length > 0);
};
// Loads the list of misspelled words from Google Drive.
// set config.enableCache to true to enable.
this.loadCache = function() {
var fileIter = DriveApp.getFilesByName(this.CACHE_FILE_NAME);
if(fileIter.hasNext()) {
this.cache = JSON.parse(fileIter.next().getBlob().getDataAsString());
} else {
this.cache = { incorrect : {} };
}
}
if(config.enableCache) {
this.loadCache();
}
// Called when you are finished with everything to store the data back to Google Drive
this.saveCache = function() {
var fileIter = DriveApp.getFilesByName(this.CACHE_FILE_NAME);
if(fileIter.hasNext()) {
fileIter.next().setContent(JSON.stringify(this.cache));
} else {
DriveApp.createFile(this.CACHE_FILE_NAME, JSON.stringify(this.cache));
}
}
}
}