0

对此问题的后续问题 -如何使用 UrlfetchApp 修复无效的 url 错误?

这是从以下网址下载 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

这是一份按计划提交的报告,我需要获取这些数据并将其放入谷歌表格中。这是一个很大的报告,所以我需要清除工作表上的所有现有数据并用新数据替换它。我一直在为此苦苦挣扎,可以使用一些帮助。

这是我现在拥有的代码:

// 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 文件。我不确定我在这里做错了什么 - 我一直在尝试在线关注一堆关于该主题的教程。

您的帮助将不胜感激!

4

1 回答 1

2

当您从下载链接获取 blob 时,它可能不包含正确的 mimeType 信息

解决方法

分两步执行请求

  1. 步骤将文件另存为云端硬盘中的 Excel
  2. 将 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,
    parents: [{id: DataFolderId}],
  }  
  const file2 = Drive.Files.insert(fileInfo2, excelFile2); 
  const id2 = file2.id;
  spreadsheet(id2)
  
}
于 2020-08-17T15:59:57.053 回答