尝试将csv文件从Gmail导出到Google表格中

时间:2018-11-05 22:28:24

标签: csv google-apps-script google-sheets

这是我正在尝试的代码,但是没有任何反应。我使用了另一个脚本直接从URL中提取文件,并且有效。有什么想法吗?

function importCSVFromGmail() {
  
  var threads = GmailApp.search("from:help@zemanta.com");
  var message = threads[0].getMessages()[0];
  
//Retrieves a URL from a HTML string from an href. Only applicable if there is only one link in the string
function GetHrefURLsFromString(string){
  var href = string.match(/href="([^"]*)/)[1];
  if(href){
    return href;
  } else {
    throw "No URL Found"
  } 
}
  //Gets a CSV from a provided link, and parses it.
function GetCSVFromLink(link){
  var urlData = UrlFetchApp.fetch(link);
  if(urlData.getBlob().getContentType() == 'csv'){
    var stringData = urlData.getContentText();
    var escapedStringData = stringData.replace(/(?=["'])(?:"[^"\\]*(?:\\[\s\S][^"\\]*)*"|'[^'\\]\r\n(?:\\[\s\S][^'\\]\r\n)*')/g, '\r\n');
    var CSV = Utilities.parseCsv(escapedStringData);
    return CSV;    
  }
  
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
  
  }
}

0 个答案:

没有答案