-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
110 lines (91 loc) · 3.54 KB
/
index.ts
File metadata and controls
110 lines (91 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Graph API
import { ClientSecretCredential } from "@azure/identity";
import { AzureIdentityAuthenticationProvider } from "@microsoft/kiota-authentication-azure";
import {
createGraphServiceClient,
GraphRequestAdapter,
} from "@microsoft/msgraph-sdk";
import "@microsoft/msgraph-sdk-users"; // For user-related types
import "@microsoft/msgraph-sdk-drives"; // For group-related types
import { HttpMethod } from "@microsoft/kiota-abstractions";
import { createWorkbookRangeFromDiscriminatorValue } from "@microsoft/msgraph-sdk/models/index.js";
// Authentication
const credential = new ClientSecretCredential(
Bun.env.TENANT_ID!,
Bun.env.CLIENT_ID!,
Bun.env.CLIENT_SECRET!,
);
const authProvider = new AzureIdentityAuthenticationProvider(credential, [
"https://graph.microsoft.com/.default",
]);
const requestAdapter = new GraphRequestAdapter(authProvider);
// client
const client = createGraphServiceClient(requestAdapter);
// Obtener drive por usuario
const drive = await client.users.byUserId("ID_OR_EMAIL").drive.get();
if (!drive) throw new Error("No drive found");
if (!drive.id) throw new Error("No drive id found");
// Obtener los items del root del drive por drive id
const items = await client.drives.byDriveId(drive.id).items.byDriveItemId(
"root",
)
.children.get();
if (!items) throw new Error("No items found");
if (!items.value) throw new Error("No items value found");
// Filtrar items por nombre
// const filteredItems = await client.drives.byDriveId(drive.id).items
// .byDriveItemId(
// "root",
// ).searchWithQ("ANA").get();
// for (const element of items.value) {
// console.log(element.name);
// }
// Para Crear un item en el root del drive
const file = await Bun.file("./test.txt").arrayBuffer();
// Se debe especificar el archivo hijo, no solo la carpeta padre
const result = await client.drives.byDriveId(drive.id).items.byDriveItemId(
"root",
).children.byDriveItemId1("test.txt").content.put(file);
// Para Extraer la informacion de un Excel guardado en OneDrive
const wb = await client.drives.byDriveId(drive.id).items.byDriveItemId(
"ITEM_ID",
)
.workbook
.worksheets.byWorkbookWorksheetId("SHEET_NAME_OR_ID").usedRange.get();
if (!wb) throw new Error("No workbook found");
if (!wb.address) throw new Error("No workbook address found");
if (!wb.values) throw new Error("No workbook values found");
// Implementar una función para convertir los valores del workbook a una matriz nativa de JavaScript
const matrix = (wb.values.value as any[]).map((r) =>
((r?.value as any[]) ?? []).map((c: any) => (c ? c.value : undefined))
);
// Ejemplo de uso de la matriz
for (const row of matrix) {
const col1 = row[0]; // Primera columna
const col2 = row[1]; // Segunda columna
// ... y así sucesivamente
}
// Actualizar valores en un rango específico
const ws = client.drives
.byDriveId(drive.id)
.items.byDriveItemId("ITEM_ID")
.workbook.worksheets.byWorkbookWorksheetId("SHEET_NAME_OR_ID");
const rangeBuilder = ws.rangeWithAddress("A1:B2");
const req = rangeBuilder.toGetRequestInformation();
req.httpMethod = HttpMethod.PATCH;
// Body REST esperado: { values: [[...], [...]] }
const values = [
["Nuevo Valor 1", "Nuevo Valor 2"],
["Nuevo Valor 3", "Nuevo Valor 4"],
];
req.setContentFromScalar(
requestAdapter,
"application/json",
JSON.stringify({ values }),
);
// Ejecutar y devolver el rango actualizado
// await requestAdapter.send(
// req,
// createWorkbookRangeFromDiscriminatorValue,
// undefined,
// );