docs.sheetjs.com/docz/static/gsheet/raw.mjs

38 lines
1.0 KiB
JavaScript
Raw Permalink Normal View History

2024-06-09 03:40:45 +00:00
import { google } from "googleapis";
import { read, utils } from 'xlsx';
/* Change this import statement to point to the credentials JSON file */
import creds from './sheetjs-test-726272627262.json' assert { type: "json" };
/* Change this to the spreadsheet ID */
const id = "SOME-SPREADSHEETJS-ID";
/* connect to google services */
const jwt = new google.auth.JWT({
email: creds.client_email,
key: creds.private_key,
scopes: [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive.file',
]
});
const drive = google.drive({ version: "v3", auth: jwt });
/* get XLSX export */
const file = await drive.files.export({
mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
fileId: id
});
const ab = await file.data.arrayBuffer();
/* parse with SheetJS */
const wb = read(ab);
/* print CSV data from each worksheet */
wb.SheetNames.forEach(n => {
console.log(`#### ${n}`);
console.log(utils.sheet_to_csv(wb.Sheets[n]));
console.log("");
})