diff --git a/Tests.gs.js b/Tests.gs.js index e0d90da..51fa851 100644 --- a/Tests.gs.js +++ b/Tests.gs.js @@ -24,6 +24,12 @@ function _runTests() { var checkResult = function(caller, result) { Logger.log(caller + ' ' + (result.length ? '✅ OK: ' + JSON.stringify(result) : '❌ Error')); + } + + var check2dArrayInputResult = function(caller, result) { + var pass = result.map(row => row.every(col => col)).every(row => row) + Logger.log(caller + ' 2D array check ' + (pass ? + '✅ 2D array OK: ' + JSON.stringify(result) : '❌ 2D array Error')); }; var project = 'en.wikipedia'; @@ -65,6 +71,7 @@ function _runTests() { article.replace('en:', ''))); checkResult('WIKIDATAQID', WIKIDATAQID(article)); + check2dArrayInputResult('WIKIDATAQID', WIKIDATAQID([[article], [article]])); checkResult('WIKIDATAQID', WIKIDATAQID(article.replace('en:', ''))); checkResult('WIKIDATALOOKUP', WIKIDATALOOKUP('P298', 'AUT')); diff --git a/Wikipedia.gs.js b/Wikipedia.gs.js index d5790b0..19028a5 100644 --- a/Wikipedia.gs.js +++ b/Wikipedia.gs.js @@ -1408,45 +1408,50 @@ function WIKISEARCH(query, opt_didYouMean, opt_namespaces) { /** * Returns the Wikidata qid of the corresponding Wikidata item for a Wikipedia article. * - * @param {string} article The article in the format "language:Query" ("de:Berlin") to get the Wikidata qid for. - * @return {string} The Wikidata qid. + * @param {string|string[]} article The article(s) in the format "language:Query" ("de:Berlin") to get the Wikidata qid for. + * @return {string|string[]} The Wikidata qid(s). * @customfunction */ -function WIKIDATAQID(article) { - 'use strict'; - if (!article) { - return ''; - } - var results = []; - try { - var language; - var title; - if (article.indexOf(':') !== -1) { - language = article.split(/:(.+)?/)[0]; - title = article.split(/:(.+)?/)[1]; - } else { - language = DEFAULT_LANGUAGE; - title = article; - } - if (!title) { +function WIKIDATAQID(input) { + function process(article) { + 'use strict'; + if (!article) { return ''; } - var url = 'https://' + language + '.wikipedia.org/w/api.php' + - '?action=query' + - '&format=json' + - '&formatversion=2' + - '&redirects=1' + - '&prop=pageprops' + - '&ppprop=wikibase_item' + - '&titles=' + encodeURIComponent(title); - var json = JSON.parse(UrlFetchApp.fetch(url, HEADERS).getContentText()); - if (json.query.pages[0] && json.query.pages[0].pageprops.wikibase_item) { - results[0] = json.query.pages[0].pageprops.wikibase_item; + var results = ''; + try { + var language; + var title; + if (article.indexOf(':') !== -1) { + language = article.split(/:(.+)?/)[0]; + title = article.split(/:(.+)?/)[1]; + } else { + language = DEFAULT_LANGUAGE; + title = article; + } + if (!title) { + return ''; + } + var url = 'https://' + language + '.wikipedia.org/w/api.php' + + '?action=query' + + '&format=json' + + '&formatversion=2' + + '&redirects=1' + + '&prop=pageprops' + + '&ppprop=wikibase_item' + + '&titles=' + encodeURIComponent(title); + var json = JSON.parse(UrlFetchApp.fetch(url, HEADERS).getContentText()); + if (json.query.pages[0] && json.query.pages[0].pageprops.wikibase_item) { + results = json.query.pages[0].pageprops.wikibase_item; + } + } catch (e) { + // no-op } - } catch (e) { - // no-op + return results; } - return results.length > 0 ? results : ''; + return Array.isArray(input) ? + input.map(row => row.map(cell => process(cell))) : + process(input); } /**