webに落ちていたスクリプトを元にbitlyの短縮URLをgoogle spread sheetで発行できるようにスクリプトを組みたく思っております。
https://teratail.com/questions/122400
の記事を読んでやったのですが実行すると空白になってしまいます。
どうしたら良いかおしえてください。
以下が設置したスクリプトです。
/**
- Returns a shortened URL of the input.
- @param {string} longUrl The long URL to shorten.
- @return The shortened url.
/
function shortenUrl(longUrl) {
try {
var regexp = /^(https?|ftp)(://[-_.!~'()a-zA-Z0-9;/?:@&=+$,%#]+)$/;
if (typeof longUrl != 'string' || !regexp.test(longUrl)) throw new Error('Invalid args');
longUrl = Utilities.base64EncodeWebSafe(longUrl);
var response = BitlyUrlShortenerService(longUrl);
return response.data.url;
} catch(e) {
return e.toString();
}
}
/**
- Returns a analytics of the short URL.
- @param {string} shortUrl
- @return click count
*/
function analyzeShortUrl(shortUrl) {
try {
var regexp = /^https?://bit.ly/.+$/;
if (typeof shortUrl != 'string' || !regexp.test(shortUrl)) throw new Error('Invalid args');
var response = BitlyUrlAnalyticsService(shortUrl);
return response.data.link_clicks;
} catch(e) {
return e.toString();
}
}
function BitlyUrlShortenerService(longUrl) {
var accessToken = PropertiesService.getScriptProperties().getProperty('BITLY_ACCESS_TOKEN'),
apiUrl = 'https://api-ssl.bitly.com/v3/shorten?access_token='+accessToken+'&longUrl='+longUrl,
response = UrlFetchApp.fetch(apiUrl);
if (response.getResponseCode() !== 200) {
throw new Error('Unable to bit.ly API');
} else {
return JSON.parse(response);
}
}
function BitlyUrlAnalyticsService(shortUrl) {
var accessToken = PropertiesService.getScriptProperties().getProperty('BITLY_ACCESS_TOKEN'),
apiUrl = 'https://api-ssl.bitly.com/v3/link/clicks?access_token='+accessToken+'&link='+shortUrl,
response = UrlFetchApp.fetch(apiUrl);
if (response.getResponseCode() !== 200) {
throw new Error('Unable to bit.ly API');
} else {
return JSON.parse(response);
}
}
あなたの回答
tips
プレビュー