簡體   English   中英

Apps 腳本:CSV 附件到 Google 表格錯誤 - 無法解析文本

[英]Apps Script: CSV attachment to Google Sheet error - Could not parse text

我正在運行一個簡單的 function 以從我的 Gmail 中提取 CSV 附件到 Google 表格。 它適用於我所有的其他 CSV,除了一個。

這是我正在運行的腳本:

function getCsvFromGmail() {
  // Get the newest Gmail thread based on sender and subject
  var gmailThread = GmailApp.search("from:test@gmail.com subject:\"testfile\"", 0, 1)[0];
  
  // Get the attachments of the latest mail in the thread.
  var attachments = gmailThread.getMessages()[gmailThread.getMessageCount() - 1].getAttachments();
  
  // Get and and parse the CSV from the first attachment
  var csvData = Utilities.parseCsv(attachments[0].getDataAsString());
  

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
sheet.clearContents().clearFormats();
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);

}

我收到的錯誤:

例外:無法解析文本。

這是示例數據集的鏈接: https://drive.google.com/file/d/1MNI01XK2HmZ8Nedm9-3U9mjEsls0hoqe/view?usp=sharing

該錯誤可能是由於field19上的換行符。 有沒有辦法可以添加額外的代碼行來解決這個問題?

謝謝你。

嘗試這個。 原來它不是\n而是\r

var attachment = attachments[0].getDataAsString();
attachment = attachment.replace(/\r/g," ");
  
// Get and parse the CSV from the first attachment
var csvData = Utilities.parseCsv(attachment);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM