Created
July 18, 2025 12:48
-
-
Save 599316527/44a35e99528394f7b515f0bc596f2198 to your computer and use it in GitHub Desktop.
下载飞书电子表格里文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // node-sdk使用说明:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/server-side-sdk/nodejs-sdk/preparation-before-development | |
| // 以下示例代码默认根据文档示例值填充,如果存在代码问题,请在 API 调试台填上相关必要参数后再复制代码使用 | |
| const lark = require("@larksuiteoapi/node-sdk"); | |
| const path = require("path"); | |
| const fs = require("fs-extra"); | |
| // 到这里获取 https://open.larkoffice.com/document/server-docs/docs/sheets-v3/spreadsheet-sheet/query | |
| APP_ID = process.env.APP_ID; | |
| APP_SECRET = process.env.APP_SECRET; | |
| SPREADSHEET_TOKEN = process.env.SPREADSHEET_TOKEN; | |
| USER_ACCESS_TOKEN = process.env.USER_ACCESS_TOKEN; | |
| const client = new lark.Client({ | |
| appId: APP_ID, | |
| appSecret: APP_SECRET, | |
| }); | |
| const requestOptions = lark.withUserAccessToken(USER_ACCESS_TOKEN); | |
| async function getSheets() { | |
| const data = await client.sheets.v3.spreadsheetSheet.query( | |
| { | |
| path: { | |
| spreadsheet_token: SPREADSHEET_TOKEN, | |
| }, | |
| }, | |
| requestOptions | |
| ); | |
| return data.data.sheets; | |
| } | |
| async function getRows(sheet) { | |
| const { | |
| sheet_id, | |
| grid_properties: { row_count }, | |
| } = sheet; | |
| const data = await client.request( | |
| { | |
| url: `/open-apis/sheets/v3/spreadsheets/${encodeURIComponent( | |
| SPREADSHEET_TOKEN | |
| )}/sheets/${encodeURIComponent( | |
| sheet_id | |
| )}/values/batch_get?user_id_type=open_id&value_render_option=unformatted_value`, | |
| method: "POST", | |
| data: { | |
| // 调整你的数据范围 | |
| ranges: [`${sheet_id}!A2:D${row_count}`], | |
| }, | |
| }, | |
| requestOptions | |
| ); | |
| return data.data.value_ranges[0].values; | |
| } | |
| async function downloadFile(fileToken, savePath) { | |
| const res = await client.drive.v1.media.download( | |
| { | |
| path: { | |
| file_token: fileToken, | |
| }, | |
| }, | |
| requestOptions | |
| ); | |
| res.writeFile(savePath); | |
| } | |
| async function main() { | |
| const sheets = await getSheets(); | |
| const files = []; | |
| for (const sheet of sheets) { | |
| if (sheet.hidden) { | |
| continue; | |
| } | |
| console.log(sheet.title, sheet.sheet_id, sheet.grid_properties.row_count); | |
| const rows = await getRows(sheet); | |
| for (const cells of rows) { | |
| // 根据你的数据调整 | |
| // https://open.larkoffice.com/document/server-docs/docs/sheets-v3/spreadsheet-sheet-value/cell-data-structure | |
| const [nameCell, , , fileCell] = cells; | |
| const name = nameCell[0].text.text.trim(); | |
| console.log(sheet.title, name); | |
| if (!fileCell?.[0].file) { | |
| files.push({ | |
| sheet: sheet.title, | |
| name, | |
| }); | |
| continue; | |
| } | |
| const savePath = path.join( | |
| "result", | |
| sheet.title, | |
| path.basename(name, path.extname(name)) + ".json" | |
| ); | |
| const { file_token, name: filename } = fileCell[0].file; | |
| await fs.ensureDir(path.dirname(savePath)); | |
| await downloadFile(file_token, savePath); | |
| files.push({ | |
| sheet: sheet.title, | |
| name, | |
| filename, | |
| savePath, | |
| }); | |
| } | |
| } | |
| await fs.writeJSON("result.json", files); | |
| console.table(files); | |
| } | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment