【问题标题】:GoogleJsonResponseException: API call to drive.files.insert failed with error: Bad RequestGoogleJsonResponseException:对 drive.files.insert 的 API 调用失败并出现错误:错误请求
【发布时间】:2021-01-08 18:21:22
【问题描述】:

我正在尝试触发脚本以从 Gmail 电子邮件中复制附加的 excel 文件并将其复制到 Google 工作表中。应复制任何带有“TO PROCESS”标签的电子邮件。我收到此错误消息:

GoogleJsonResponseException: API call to drive.files.insert failed with error: Bad Request

这是发生在该行的错误:

var convertedSpreadsheetId = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS}, xlsxBlob).id; //Converts Excel to "Google Sheet" in google drive and gets file Id

完整代码如下:

function emailTrigger() {

  var label = GmailApp.getUserLabelByName("TO PROCESS");

  if(label != null){
    var threads = label.getThreads();
    for (var i=0; i<threads.length; i++) {
      getExcelFile(threads[i]);
      //Process them in the order received
      threads[i].removeLabel(label);
    }
  }
}

function getExcelFile(thread) 
{
//Function to pull Weekly Schedule Excel file from Janettas email and update phone spreadsheet and calendar
  
  var messages = thread.getMessages(); //pulls messages in first thread
  var len = messages.length; //Gets number of messages in first thread
  var message = messages[len-1] //get first message in given thread
  var attachments = message.getAttachments(); // Get attachment of first message
  
  //Steps to process the attachement
  var xlsxBlob = attachments[0]; // Is supposes that attachments[0] is the blob of xlsx file.
  var convertedSpreadsheetId = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS}, xlsxBlob).id; //Converts Excel to "Google Sheet" in google drive and gets file Id
  var filename = xlsxBlob.getName(); //gets the converted files file name
  var tabName = filename.substring(13).slice(0,filename.length-18); // process the filename string into just the date to be Tab Name
  
  var sheet = SpreadsheetApp.openById(convertedSpreadsheetId).getSheets()[0]; // Location of converted Excel file -> now google sheet
  }

【问题讨论】:

  • 能问一下xlsxBlob的mimeType吗?
  • 您是否可以使用 v3 API 而不是 v2 API? v2 有插入,但 v3 有创建。
  • @Tanaike - 很抱歉,我不确定您需要我提供什么。我从另一个论坛帖子中获取了此代码。为了回答您的问题,您具体想要什么?我在哪里可以找到?
  • @MattWilbert - 查看高级 Google 服务时 - Drive API 被列为 v2。我认为这就是您所指的 API。
  • 感谢您的回复。我带来的不便表示歉意。在这种情况下,我认为当检查 xlsxBlob 的 mimeType 时,可能是您的问题的原因。所以我就问了。

标签: google-apps-script gmail attachment


【解决方案1】:

发生错误是因为特定的 Excel 电子表格已加密。代码没有问题,可以按预期使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多