我正试图触发一个脚本,从Gmail电子邮件中复制一个附加的excel文件,并将其复制到谷歌工作表中。任何带有标签"的电子邮件;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
}
发生错误的原因是特定的excel电子表格已加密。代码没有问题,可以按预期使用。