Google apps script 反向缩短的URL以在Google表单中缩短之前获取原始URL

Google apps script 反向缩短的URL以在Google表单中缩短之前获取原始URL,google-apps-script,google-sheets,google-apps,Google Apps Script,Google Sheets,Google Apps,我有一张工作表,在那里我收集基于搜索查询的链接推文。然而,Twitter给我的是每条推特的短URL格式(t.co),而不是共享的原始URL 有没有办法使用公式或谷歌代码来回溯短url并获取最终目标url?推文作者分享的原始内容 What i have | what i'm looking for --------------------------------------------------------------------------- https:/

我有一张工作表,在那里我收集基于搜索查询的链接推文。然而,Twitter给我的是每条推特的短URL格式(t.co),而不是共享的原始URL

有没有办法使用公式或谷歌代码来回溯短url并获取最终目标url?推文作者分享的原始内容

     What i have        |       what i'm looking for
---------------------------------------------------------------------------
https://t​.co/dura1sUSxm | https://www.jpost.com/Breaking-News/Russia-Saudi-Arabia-plan-deals-for-2-bln-for-Putins-visit-to-Riyadh-604200
https://t​.co/Ayy7ww8dFX | https://www.washingtonpost.com/national-security/trump-says-little-as-his-gop-allies-condemn-turkeys-incursion-into-syria/2019/10/09/c46210f6-eaab-11e9-9306-47cb0324fd44_story.html
https://t​.co/WLj6PipXkC | https://www.newsweek.com/teacher-fired-refusing-sign-pro-document-1262083
https://t​.co/UoqiqfaHup | https://www.reuters.com/article/us-environment-waste-idUSKBN1WP1RE
https://t​.co/hO9swbmeeZ | https://www.washingtonpost.com/national-security/trump-says-little-as-his-gop-allies-condemn-turkeys-incursion-into-syria/2019/10/09/c46210f6-eaab-11e9-9306-47cb0324fd44_story.html
https://t​.co/Ve8ZpCp1s1 | https://www.reuters.com/article/us-environment-waste-idUSKBN1WP1RE
答复: 是的,这可以使用
UrlFetchApp
fetch
方法,获取标题并读取
Location
属性

方法: 您可以使用
UrlFetchApp
获取包含最终URL端点的目标的标题。然而,将其放入循环中很重要,因为有时多个URL缩短服务串联在一起,因此您可能无法在一次获取后获得目标URL

代码: 我希望这对你有帮助

function getLocation(url) {
  var fetched = UrlFetchApp.fetch(url, {
    followRedirects: false
  })

  if (fetched.getHeaders().Location !== undefined) {
    return getLocation(fetched.getHeaders().Location)
  }
  return url
}