简体   繁体   English

从谷歌云存储下载 excel 文件返回一个缓冲区,我怎样才能返回实际的 excel 文件,以便我可以在我的处理程序中写入它?

[英]Downloading excel file from Google Cloud storage returns a buffer, how can I return the actual excel file so I can write to it in my handler?

I'm trying to retrieve an excel file from my bucket using the Firebase Admin SDK using the following code:我正在尝试使用 Firebase Admin SDK 从我的存储桶中检索 excel 文件,代码如下:

export const getTemplate = async (fileName: string) => {
  const filePath = admin
    .storage()
    .bucket()
    .file('sheets/template.xlsx')

  const [file] = await filePath.download()
  return file
}

I then pass what's returned from this function, to this function:然后我将从这个 function 返回的内容传递给这个 function:

export const writeToTemplate = (
  objects: any[],
  sheetNames: string[],
  file
) => {

  objects.forEach((dataObject, index) => {
    const sheetName = sheetNames[index]
    const worksheet = templateFile.Sheets[sheetName]
    utils.sheet_add_json(worksheet, dataObject)
  })
  return file
}

However this throws an error that it cannot read my file.但是,这会引发一个错误,即它无法读取我的文件。 When I console log the file it shows a buffer当我控制台记录文件时,它显示一个缓冲区

<Buffer 50 4b 03 04 14 00 06 00 08 00.........>

How can I actually write to the file?我怎样才能真正写入文件? Cheers:)干杯:)

I needed to read the Buffer with the read function from the 'xlsx' package!我需要从“xlsx”包中读取 function 来读取缓冲区!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将值从 Javascript 文件返回到 Node.js,以便我可以写入文件? - How can I return a value from a Javascript file to a Node.js so I can write to a file? 从 Google Cloud Storage 读取 Excel 文件 - Read Excel file from Google Cloud Storage 如何使用multer将文件传输到Google Cloud Storage? - How can I pipe a file using multer to Google Cloud Storage? 如何使用CurL从终端中的GCS(Google云存储)URL发送音频文件? - How can i send audio file from GCS (Google Cloud Storage) URL in terminal using CurL? 如何使用 Node API 从 Google Cloud Storage 返回指定的图像大小? - How Can I Return Specified Image Size From Google Cloud Storage With Node API? 如何将缓冲区数据从 Nodejs 中的 readable.stream 写入文件? - How can I write a buffer data to a file from readable.stream in Nodejs? 如何将内存中的multer文件缓冲区上传到Google Cloud Storage Bucket? - How to upload the multer file buffer in memory to google cloud storage bucket? 如何从CollectionFS获取文件(图像)的缓冲区 - How can I get a buffer for a file (image) from CollectionFS 帆:如何将Excel文件数据读取为json格式 - sails:How can i read excel file data into json format 如何使用angularjs从物理路径下载Excel文件 - How can i download Excel file from physical path using angularjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM