1

我是 Apps 脚本编码的新手。

我每周都会收到来自特定网站的 gmail 消息中的外部链接,该链接允许我下载 Excel 数据提取(xlsx 文件)。最初,当我单击链接时,它会重定向到外部网站登录页面,我必须在其中输入我的用户名和密码。完成后,它会自动在 Chrome 中下载文件。

我想做的事:通过创建与标签同名的文件夹,循环浏览电子邮件,并将从 URL 中提取的 Excel 数据保存到我的 Google Drive 文件夹(或更好的 Team Drive 文件夹)中。最好将其保存为 Google 电子表格格式(如果不是,我可以使用 Excel xlsx 格式)。

我创建了一个 Google 电子表格来记录此链接、电子邮件主题和收到日期,并且最好将状态列设置为“已处理”。

我在电子邮件中收到的链接是这种格式:

https://XXXXX-XXX.XXXXXXX.com/XXXXXXXWeb/ReportDownloadServlet?outputInd=P&executionId=1234567

最初,当我单击该链接时,它会重定向到外部网站登录页面,我在其中输入用户名和密码并单击“记住我”:

https://abcdefg.hijklmn.com/gateway/logon.htm?TYPE=33619969&REALMOID=06-0008fcbc-145a-1be3-b1b7-05e50a27c0f4&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=-SM-UmRXxeTjx0wukOHolEZ%2fuji7%2f9Iznin6POLHraH%2b4Pc30o%2fZFVkAWVfTNIh2ZF8V&TARGET=-SM-HTTPS%3a%2f%2fXXXXX--XXX%2XXXXXXX%2ecom%2fXXXXXXXWeb%2fReportDownloadServlet%3foutputInd%3dP%26executionId%3d1234567

在 Google 电子表格容器脚本编辑器中,我编写了以下代码:

function myFunction() {
  var start = 0;
  var inc = 100;
  var k = 0;
  var ss = SpreadsheetApp.openById("*****-*********_****************************");
  var sheet = ss.getSheetByName("Sheet1");
  var label = GmailApp.getUserLabelByName("SOME_LABEL");
  var arr=[];
  
  do {    
//  var now = new Date();
  var threads = label.getThreads(start,inc);

  for (var i = 0; i<threads.length; i++)
  {
    var messages = threads[i].getMessages();

    for (var j = 0; j < messages.length; j++)
    {
      var msg = messages[j].getPlainBody().toString(); 
      
      var re1='.*?';	// Non-greedy match on filler
      var re2='((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))';	// HTTP URL 1
      var pattern = new RegExp(re1+re2,["i"]);
      var matches = pattern.exec(msg);
      var url = matches[1].replace(">","");
      
      var user = "USERNAME";
      var password = "PASSWORD";

      //var headers = {
      //              "Accept": "application/xml",
      //              "Content-Type": "application/xml",
      //              "Authorization": "Basic "+ Utilities.base64Encode(user+":"+password)
      //      };
      
      //defining method to download file
      //var options = {
      //  "method" : "get",
      //  "headers" : headers,
      //  "muteHttpExceptions" : true
      // };
      
      // var response = UrlFetchApp.fetch(url,options);
      var response = UrlFetchApp.fetch(url);
      var blob = response.getBlob();
      
      var folders = DriveApp.getFoldersByName('Team Drives');
      while (folders.hasNext()) {
        var folder = folders.next();
      }
      var subfolder = folder.createFolder("SOME_LABEL");
      var file = subfolder.createFile(response);

      var file = folder.createFile(response);

      
      var dat = messages[j].getDate();
 //      sheet.appendRow([k, msg, sub, dat]);
      k++;
//      arr[k-1] = [[k,msg,sub,dat]];
    }
//      threads[i].removeLabel(label);
  
  }
  start += inc;
  
}  while (threads.length == inc);
  sheet.insertRowsAfter(1, k);
}

我能够遍历我的 Gmail 文件夹(标签)中的电子邮件并正确提取链接。现在,当我尝试使用 UrlFetchApp 时,我得到的响应如下:

ReportDownloadServlet.html

打开时基本上是登录页面,而不是 Excel 文件。

我无法识别要从 Chrome 开发人员扩展 (F12) 传递给 UrlFetchApp() 方法的选项和标题(参数)。

有人可以帮我确定正确的参数吗?

另外,我从 Chrome 开发者扩展(控制台日志)中注意到的一些事情:

资源解释为 Document,但使用 MIME 类型 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 传输:“ https://XXXXX-XXX.XXXXXXX.com/XXXXXXXWeb/ReportDownloadServlet?outputInd=P&executionId=1234567 ”。

这是我从 Network > Selected the URL link > Clicked Headers 选项卡复制的。

Request URL:https://XXXXX-XXX.XXXXXXX.com/XXXXXXXWeb/ReportDownloadServlet?outputInd=P&executionId=1234567
Request Method:GET
Status Code:200 OK
Remote Address:123.123.123.123:443
Referrer Policy:no-referrer-when-downgrade


Response Headers
Connection:Keep-Alive
Content-Disposition:attachment; filename="SOME_FILENAME.xlsx";
Content-Encoding:gzip
Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Date:Wed, 21 Jun 2017 18:25:48 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.21 (Unix)
SESSIONLOGID:56097422
Set-Cookie:XXXXX-XXX_cookie=R951123141; path=/; expires=Thu, 22-Jun-2017 07:20:48 GMT
Set-Cookie:SMSESSION=ZKpKqhPOkqIDQqlp1pbIamKby5gmAGnhwOgmHlckXTd3GWGrqlSG4ZHKyJW1JpRrg2TOR6WB9Q+8q8D4Fd/PwsNyCL6ItmxDJfMEF1BewbbL8mPouRLt8jgPLFMjt3De9J5WwIHhxXMI67ua3t+KSKvnqPk4G6LMjOOy3JcDKWz/J6l2nMXCrTq7g6aAxH5NkJWcliLLHLIrpBPz+v8XT729gnuf4yXCpe3frz+wLNcY+t0YDKcIduCtgEo0QuZ2cYqub6fenZxSbvg+Zhla1OCduU9+/Tb6CyM6R/hl+fJ1CT7pGcWM8MGlapS+xV2UCOe0q0I8syojlH3lTwn0jWHJ0aOF6jnUPkrIsvunFD9g2JdGasCg21O18Gn94pUbgpQX4YeAdIUmXo7HQ7K6mnbXOMhsmGhEGDv8c6YGU57mD0owxncKpm1XgZFW70Fo1vztxxQorP23ui8kpdyz75VhDeebYftyTv340Y/2APiVOG7ojXVq+2Tjcky9TgPdk1EVMWCeIiqjroWBG5IiCvbLol7Wl1JSKg2i/q+zJmzlyn4vo9db2wffBG/JnuY2qnd9urngzI9XGNPEugymRVfyi7y6W73Qdvedry3/Qd8IDh9LHZ1r4U5KfNW6vir5iewaVcVRfHJw6MYp2iLpvhIVvqvxZCDt9/7juhB6VmRVxjuyhEbGKXwz7DLkt5rzr3R/eY5L/ztJis9ZNhtU8wzQHg2S6F0AU0AyzIHKSRKVBy+MtOSbPgb8B9nRQYt04BCI/Kb3ehIzISbg9Tm26Q4v5J1EWo5rBsbNefOfpFuqM4Y6uN0X1Hq2BHlAQGtyB6n3A3Bekb8mhcjmDeotWj9MPpSV4qNhGgtLOeuxdkrXlLuqrUsVNVLue1VyvwuY4bWn4PKjYAlahd+THyjT+GkXN9pnxLDGTo9ZfsueXZOqXK16xtClAub0Ff/hjrrOD9g3u4wLVHg4NARi3IayFAgW4hcgSl1EzyhJ059vJKIiiBukSn9xi0tL2SY6cqIBskDv64hLl+86/82vxndW5LSG3zwQp4ZWbzjHrAdgIgnY/7BHthX/hRseynPxhXtIey7+z8FYGBvY93Gg8RoqEHmM6yHB4JXs52FpJj+R/GfSX/4I6ArjsguQLU9hU1Z7Lq0EB9IdXwUqdQ8/2NAG4rrJ88BuAYeAFd3blg3vN423dQMePOv9iTbLZvGkR5mnYzRadETn6qidxxCkj7MMkLhxhMddatWSLBENyC+1SKfSHWK7Jno+/zNRa+qOWyRR9cGdYlDgBhEBqFRNxTFAaq+1PSEAoTDs7/alPeLHFd6JynTqHSCjVLcYcBEVLUxjKfvSCknwjdOjjK8D97cJqi72whvRSlqSMWNwvcp2GvHt27IVuJ5aCJRFNDa1yzPZsGq3wQxOTmyPilDBDbuk+VgwF83OVjjBlXRMAo4+8BeINcX7vSaM7jgjd3wSlBHEduRDU/LyutIbRi8PuyEzAX7xBj0JqXOlm/6Cm5l2+krtY4w9ZljlSkUyFi3V6O4T; path=/; domain=.XXXXXXX.com
Transfer-Encoding:chunked
Vary:Accept-Encoding,User-Agent
X-Powered-By:Servlet/2.5 JSP/2.1


Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-IN,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:_ga=GA1.2.1347973701.1497504891; XXXXX-XXX_cookie=R951123141; SMSESSION=+ebdOREr4ZKBRN+XWlS2UQ7BC9YZ4uwIyRnAUJx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Rg25KkhIh6vTYeCXDiVP1itXeJ+WtUM14OTUFzj217DD0tcKhUAkdO3Xsan5G0j2VBeAhCwkIKFtedcZmIRfqVFqN1ghbkvco3TUwWJv/SZUh/97prNv5a0vu+g1w8ZOa5SP7bWNLmxBrhx4J7go9dbfwZVZpfjpqPvYvPYcb8fnB6S/oBOumE5ppJUh3wRVanHZA+JVZJI9qYNybu+MC9Nno4jdfho/8u1ClYy5+QhPQdSmKUHi+a+UlPNkwwQGnLindNKcGj8kasDUHe77OkZr84PDeh0c7j5ePVYJ6Ye8BrENzTPQst9MnxeNYQC0R9racVo3xI/w0pEoFAKD14tTI2IcH6IFsw8s7Z8fVWm8BnJeqjDO/GZqjSPNVEK9pd4/HMYxuWZigi/L4uS2kRbmB3OznrtPHsEu88ICyxqofuk; Portal-Organisation-Cookie=8a5bbfef1999ec8643314bd79ce71218
Host:XXXXX-XXX.XXXXXXX.com
Referer:https://abcdefg.hijklmn.com/gateway/logon.htm?TYPE=33619969&REALMOID=06-0008fcbc-145a-1be3-b1b7-05e50a27c0f4&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=-SM-UmRXxeTjx0wukOHolEZ%2fuji7%2f9Iznin6POLHraH%2b4Pc30o%2fZFVkAWVfTNIh2ZF8V&TARGET=-SM-HTTPS%3a%2f%2fXXXXX--XXX%2XXXXXXX%2ecom%2fXXXXXXXWeb%2fReportDownloadServlet%3foutputInd%3dP%26executionId%3d1234567
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

Query String Parameters
outputInd:P
executionId:1234567

这有帮助吗?

4

0 回答 0