如何解决如何使用Google Apps脚本从URL将Excel数据获取到Google表格中?
关注该问题-How to fix invalid url error using UrlfetchApp?
这是从以下网址下载excel数据的网址:
https://corvo-reports.s3.amazonaws.com/TRESAH/2020-08-16/45d32ff8-bccd-4c16-8916-6c19c28f2f3c%402020-08-16%2017%3A30%3A00.0/Sponsored%20Products%20Search%20term%20report%20-%20Scotch%20Brite.xlsx?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20200816T174421Z&X-Amz-SignedHeaders=host&X-Amz-Expires=604799&X-Amz-Credential=AKIAY2R3XYZC46Q4PK5E%2F20200816%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=726ca9b98cd766966b756bc708b780377ff62e5bbfec8c09b97294bfb0cd63f7
这是一份按计划提交的报告,我需要获取这些数据并将其放在Google工作表中。这是一个很大的报告,因此我需要清除工作表上的所有现有数据并将其替换为新数据。我一直在为此苦苦挣扎,可以寻求帮助。
这是我现在拥有的代码:
// Globals
const DataFolderId = 'XXXXX'
const label = 'YYYYY'
/*
* Download the excel file and put it in a google sheet
*
* @return : Hopefully,it adds the data to the google sheet
*/
function excelToSheets() {
// Get the link to the excel file
const url = getDownloadLink(label)
const excelFile = UrlFetchApp.fetch(url).getBlob()
const filename = 'Report: ' + Utilities.formatDate(new Date(),"IST","yyyy-MM-dd")
// Download the file to google drive
const fileInfo = {
title: filename,mimeType: "MICROSOFT_EXCEL","parents": [{'id': DataFolderId}],}
// Currently,this adds a zip file to google drive,instead of an excel/google sheet file
const file = Drive.Files.insert(fileInfo,excelFile,{convert: true})
// Assign the id of the downloaded file to a variable
const id = file.id
// Put data in a spreadsheet - this does not work
spreadsheet(id)
}
/*
* Helper function to put data in the google sheet
*/
function spreadsheet (id) {
const source = SpreadsheetApp.openById(id)
const destination = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('data')
destination.clearContents()
source.copyTo(destination)
}
/*
* Get the download link from the gmail label
*
* @return : The link from the most recent email
*/
function getDownloadLink (label) {
const lookupLabel = GmailApp.getUserLabelByName(label)
const thread = lookupLabel.getThreads()[0]
const message = thread.getMessages()[0]
const data = message.getPlainBody()
const regExp = new RegExp('[\n\r].*Download:\\s*([^\n\r]*)')
const link = regExp.exec(data)[1].trim()
return link
}
当前,excelToSheets()
函数从链接中获取Excel文件,并在Google驱动器中添加一个zip文件。我不确定自己在做什么错-我只是一直在尝试在线阅读有关该主题的大量教程。
非常感谢您的帮助!
解决方法
当您从下载链接中获取Blob时,它可能不包含正确的mimeType信息
解决方法
分两步执行请求
- 将文件另存为Excel并保存在云端硬盘中
- 将Excel文件转换为Google表格
样品
function excelToSheets() {
const url = getDownloadLink(label);
const excelFile = UrlFetchApp.fetch(url).getBlob();
const filename = 'Report: ' + Utilities.formatDate(new Date(),"IST","yyyy-MM-dd")
const fileInfo = {
title: filename,mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",parents: [{id: DataFolderId}],}
const file = Drive.Files.insert(fileInfo,excelFile)
const id = file.id;
const excelFile2 = DriveApp.getFileById(id).getBlob();
const fileInfo2 = {
title: filename,mimeType: MimeType.GOOGLE_SHEETS,}
const file2 = Drive.Files.insert(fileInfo2,excelFile2);
const id2 = file2.id;
spreadsheet(id2)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。