我需要从xlsx下载所有的myFolder文件,我有两种方法getExcelSheets来获得所有excel工作表列表
然后使用这些信息通过调用@microsoft.graph.downloadUrl方法下载文件,但是我没有将其转换为xlsx格式。
import { Client } from "@microsoft/microsoft-graph-client";
import axios from "axios";
import * as MicrosoftGraph from "@microsoft/microsoft-graph-types";
export async function getExcelSheets(
template_name: string,
msgraph_client: Client,
): Promise<MicrosoftGraph.DriveItem[]> {
const result = await msgraph_client
.api(
`drives/{driver_id}/root:/myFolder/${template_name}:/children`
)
.get();
return result.value as MicrosoftGraph.DriveItem[];
}
export async function getFileContentById(download_url: string): Promise<any> {
const response = await axios.get(download_url);
return response;
}关于如何获得文件并将其转换为xlsx的任何提示,我都是使用此方法将缓冲区转换为xlsx
xlsx.read(file, { type: "buffer" })发布于 2021-08-18 09:49:28
您在getFileContentById中进行的Axios调用将收到一个流,您可以将该流写入文件或转换为xlsx,如下所示,
export async function getFileContentById(download_url: string): Promise<any> {
const writer = fs.createWriteStream('file.xlsx');
return axios({
method: 'get',
url: download_url,
responseType: 'stream',
}).then(response => {
// Save the file and read later
response.data.pipe(writer);
// OR Convert the binary to xlsx usibng the library
const sheet = XLSX.read(response.data, { type: "buffer" });
console.log(sheet)
/*
{
SheetNames: [ 'Sheet1' ],
Sheets: { Sheet1: { A1: [Object], '!ref': 'A1' } }
}
*/
});
}https://stackoverflow.com/questions/68795541
复制相似问题