前提・実現したいこと
GASでbitlyの短縮URLをスプレッドシート上で自動で発行する関数を組もうとしています。
下記のコードで短縮URLは発行できているのですが、元のURLの一部のみを認識しているようで、
URL全体を短縮してくれません。
どうやら、&以下を認識してくれていないようで、
おそらく正規表現の指定が誤っているのかと思うのですが、見たところ間違っている箇所が見当たりません。
解決方法がわかりましたら、ご教示いただけますでしょうか。
発生している問題・エラーメッセージ
元URL https://google.com?creative_id=18512&token=ead17d00f5465b1fe67800de84e9d15c4ce58174&utm_medium=twitter_org&utm_source=gis19 短縮URL http://bit.ly/2qHyNpW 短縮URL展開 https://www.google.com/?creative_id=18512
該当のソースコード
GAS
1/** 2 * Returns a shortened URL of the input. 3 * 4 * @param {string} longUrl The long URL to shorten. 5 * @return The shortened url. 6 */ 7function shortenUrl(longUrl) { 8 try { 9 var regexp = /^(https?|ftp)(://[-_.!~*\'()a-zA-Z0-9;/?:\@&=+$,%#]+)$/; 10 if (typeof longUrl != 'string' || !regexp.test(longUrl)) throw new Error('Invalid args'); 11 var response = BitlyUrlShortenerService(longUrl); 12 return response.data.url; 13 } catch(e) { 14 return e.toString(); 15 } 16} 17 18/** 19 * Returns a analytics of the short URL. 20 * 21 * @param {string} shortUrl 22 * @return click count 23 */ 24 25function BitlyUrlShortenerService(longUrl) { 26 var accessToken = PropertiesService.getScriptProperties().getProperty('BITLY_ACCESS_TOKEN'), 27 apiUrl = 'https://api-ssl.bitly.com/v3/shorten?access_token='+accessToken+'&longUrl='+longUrl, 28 response = UrlFetchApp.fetch(apiUrl); 29 if (response.getResponseCode() !== 200) { 30 throw new Error('Unable to bit.ly API'); 31 } else { 32 return JSON.parse(response); 33 } 34} 35 36
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/04/18 08:16
2018/04/18 09:06
2018/04/18 09:33
退会済みユーザー
2018/04/18 14:29