如何解决GoogleJsonResponseException:对 drive.files.insert 的 API 调用失败,错误为:错误请求
我正在尝试触发脚本以从 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
}
解决方法
发生错误是因为特定的 Excel 电子表格已加密。代码没有问题,可以按预期使用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。