如何使用UrlfetchApp修复无效的URL错误? [英] How to fix invalid url error using UrlfetchApp?

查看:75
本文介绍了如何使用UrlfetchApp修复无效的URL错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计划报告,该报告以下载链接的形式出现在我的Gmail收件箱中.我需要弄清楚如何下载数据并将其添加到Google表格电子表格中.

I have a scheduled report that comes to my Gmail inbox as a download link. I need to figure out how to download the data and add it to Google Sheets spreadsheet.

我已经设置了脚本以从Gmail中提取数据:

I've set up the script to pull the data from Gmail:

function getDownloadLink() {
  
  var label = GmailApp.getUserLabelByName('test')
  var threads = label.getThreads()
  
  for(var t in threads) {
    var messages = threads[t].getMessages()
    
    for(var i in messages) {
      var data = messages[i].getPlainBody()
      var regExp = new RegExp('[\n\r].*Download:\s*([^\n\r]*)')
      var link = regExp.exec(data)[1]
      
      return link
    }
  }
}

接下来,基于一些Google搜索,我尝试使用 UrlFetchApp.fetch 方法从链接中获取数据,但无法这样做.我收到与该URL无效有关的错误.我的猜测是我没有正确使用regex函数提取URL (我在网上找到了regex表达式,并且似乎可以在regex101.com上使用)

Next, based on some google searches, I tried to use the UrlFetchApp.fetch method to get the data from the link but was unable to do so. I got an error related to the URL not being a valid URL. My guess is that I'm not using the regex function correctly to extract the URL (I found the regex expression online and it seemed to work on regex101.com)

这是存储在 var data 中的数据:

[20-08-09 11:28:08:054 PDT] The Amazon Advertising report you requested is now available.

If you no longer need this report to be generated or if you do not need it to be generated as often, please update or delete your subscription.  To manage your report subscriptions, access the Amazon Advertising Report Center.
* Note, you will need to sign into your account before accessing Amazon Advertising.

Report name: SP KW - July'20 XXXXXX
Generated on: Sunday, 9 August, 2020
* This download link expires on Sunday, 16 August, 2020

Download: https://corvo-reports.s3.amazonaws.com/TRESAH/2020-08-09/f1a86607-1558-427b-8976-370438ceb182%402020-08-09%2017%3A19%3A00.0/SP%20KW%20-%20July%2720%20XXXXX%20XXXXXX.xlsx?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20200809T172556Z&X-Amz-SignedHeaders=host&X-Amz-Expires=604800&X-Amz-Credential=AKIAY2R3XYZC46Q4PK5E%2F20200809%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=bde1f7039b4a005a6f531129183eed553e88c50999999152cca827bbe1838b

我已将该链接作为其机密数据进行了一些变形,但这实际上是电子邮件的格式.该链接下载一个.xlsx文件.如何从邮件正文中的链接获取数据,然后将数据获取到Google表格中?

I've distorted the link slightly as its confidential data but that is essentially the format of the email. The link downloads a .xlsx file. How to get the data from the link in the mail body and then get the data into a Google Sheet?

推荐答案

当我看到示例数据的 regExp var数据时,我认为在您的情况下,检索到的URL类似于 https://corvo-reports.s3.amazonaws.com ... .第一个字符带有空格.

When I saw your regExp and var data of sample data, I think that in your case, the retrieved URL is like https://corvo-reports.s3.amazonaws.com.... The 1st character has a space.

    var regExp =新的RegExp('[\ n \ r].* Download:\ s *([^ \ n \ r] *)') regExp >是/[\ n \ r].*下载:s *([^ \ n \ r] *)/

我认为这可能是您遇到问题的原因.那么下面的修改如何?

I think that this might be the reason of your issue. So how about the following modification?

var regExp = new RegExp('[\n\r].*Download:\s*([^\n\r]*)')

收件人:

var regExp = new RegExp('[\n\r].*Download:\\s*([^\n\r]*)');

  • 通过此修改,可以从示例数据中检索 https://corvo-reports.s3.amazonaws.com ... .
    • 我不确定您的 https://corvo-reports.s3.amazonaws.com ... 的URL是否有效.因此,如果无法使用该URL,请再次确认该URL.
    • 此外,如果无法使用上述修改,请尝试使用 var link = regExp.exec(data)[1] .trim()而不是 var link = regExp.exec(data)[1] .
    • I'm not sure whether your URL of https://corvo-reports.s3.amazonaws.com... is valid. So if the URL cannot be used, please confirm the URL again.
    • Also, when above modification cannot be used, please try var link = regExp.exec(data)[1].trim() instead of var link = regExp.exec(data)[1].

    这篇关于如何使用UrlfetchApp修复无效的URL错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆