54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
|
|
import { config } from 'dotenv';
|
|
import { Client } from '@microsoft/microsoft-graph-client';
|
|
|
|
config({ path: '/home/admin/secrets/.env' });
|
|
|
|
async function getGraphToken() {
|
|
const tokenEndpoint = `https://login.microsoftonline.com/${process.env.TENANT_ID}/oauth2/v2.0/token`;
|
|
const params = new URLSearchParams();
|
|
params.append('client_id', process.env.CLIENT_ID!);
|
|
params.append('client_secret', process.env.CLIENT_SECRET!);
|
|
params.append('scope', 'https://graph.microsoft.com/.default');
|
|
params.append('grant_type', 'client_credentials');
|
|
|
|
const response = await fetch(tokenEndpoint, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: params,
|
|
});
|
|
|
|
const data = await response.json();
|
|
return data.access_token;
|
|
}
|
|
|
|
async function inspectTables() {
|
|
const token = await getGraphToken();
|
|
const client = Client.init({
|
|
authProvider: (done) => {
|
|
done(null, token);
|
|
},
|
|
});
|
|
|
|
const driveId = process.env.DRIVE_ID;
|
|
const itemId = process.env.ITEM_ID;
|
|
const baseUrl = `/drives/${driveId}/items/${itemId}/workbook/tables`;
|
|
|
|
console.log(`Inspecting tables in file: ${itemId}`);
|
|
|
|
try {
|
|
const res = await client.api(baseUrl).get();
|
|
const tables = res.value;
|
|
|
|
for (const table of tables) {
|
|
console.log(`\nTable: ${table.name}`);
|
|
const cols = await client.api(`${baseUrl}/${table.name}/columns`).get();
|
|
console.log(`- Column count: ${cols.value.length}`);
|
|
}
|
|
} catch (err) {
|
|
console.error('Error:', err.message);
|
|
}
|
|
}
|
|
|
|
inspectTables();
|