@starfysh/gdrive-mcp 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,618 @@
1
+ import { UserError } from 'fastmcp';
2
+ import { hexToRgbColor, NotImplementedError } from './types.js';
3
+ // --- Constants ---
4
+ const MAX_BATCH_UPDATE_REQUESTS = 50; // Google API limits batch size
5
+ // --- Core Helper to Execute Batch Updates ---
6
+ export async function executeBatchUpdate(docs, documentId, requests) {
7
+ if (!requests || requests.length === 0) {
8
+ // console.warn("executeBatchUpdate called with no requests.");
9
+ return {}; // Nothing to do
10
+ }
11
+ // TODO: Consider splitting large request arrays into multiple batches if needed
12
+ if (requests.length > MAX_BATCH_UPDATE_REQUESTS) {
13
+ console.warn(`Attempting batch update with ${requests.length} requests, exceeding typical limits. May fail.`);
14
+ }
15
+ try {
16
+ const response = await docs.documents.batchUpdate({
17
+ documentId: documentId,
18
+ requestBody: { requests },
19
+ });
20
+ return response.data;
21
+ }
22
+ catch (error) {
23
+ console.error(`Google API batchUpdate Error for doc ${documentId}:`, error.response?.data || error.message);
24
+ // Translate common API errors to UserErrors
25
+ if (error.code === 400 && error.message.includes('Invalid requests')) {
26
+ // Try to extract more specific info if available
27
+ const details = error.response?.data?.error?.details;
28
+ let detailMsg = '';
29
+ if (details && Array.isArray(details)) {
30
+ detailMsg = details.map(d => d.description || JSON.stringify(d)).join('; ');
31
+ }
32
+ throw new UserError(`Invalid request sent to Google Docs API. Details: ${detailMsg || error.message}`);
33
+ }
34
+ if (error.code === 404)
35
+ throw new UserError(`Document not found (ID: ${documentId}). Check the ID.`);
36
+ if (error.code === 403)
37
+ throw new UserError(`Permission denied for document (ID: ${documentId}). Ensure the authenticated user has edit access.`);
38
+ // Generic internal error for others
39
+ throw new Error(`Google API Error (${error.code}): ${error.message}`);
40
+ }
41
+ }
42
+ // --- Text Finding Helper ---
43
+ // This improved version is more robust in handling various text structure scenarios
44
+ export async function findTextRange(docs, documentId, textToFind, instance = 1) {
45
+ try {
46
+ // Request more detailed information about the document structure
47
+ const res = await docs.documents.get({
48
+ documentId,
49
+ // Request more fields to handle various container types (not just paragraphs)
50
+ fields: 'body(content(paragraph(elements(startIndex,endIndex,textRun(content))),table,sectionBreak,tableOfContents,startIndex,endIndex))',
51
+ });
52
+ if (!res.data.body?.content) {
53
+ console.warn(`No content found in document ${documentId}`);
54
+ return null;
55
+ }
56
+ // More robust text collection and index tracking
57
+ let fullText = '';
58
+ const segments = [];
59
+ // Process all content elements, including structural ones
60
+ const collectTextFromContent = (content) => {
61
+ content.forEach(element => {
62
+ // Handle paragraph elements
63
+ if (element.paragraph?.elements) {
64
+ element.paragraph.elements.forEach((pe) => {
65
+ if (pe.textRun?.content && pe.startIndex !== undefined && pe.endIndex !== undefined) {
66
+ const content = pe.textRun.content;
67
+ fullText += content;
68
+ segments.push({
69
+ text: content,
70
+ start: pe.startIndex,
71
+ end: pe.endIndex
72
+ });
73
+ }
74
+ });
75
+ }
76
+ // Handle table elements - this is simplified and might need expansion
77
+ if (element.table && element.table.tableRows) {
78
+ element.table.tableRows.forEach((row) => {
79
+ if (row.tableCells) {
80
+ row.tableCells.forEach((cell) => {
81
+ if (cell.content) {
82
+ collectTextFromContent(cell.content);
83
+ }
84
+ });
85
+ }
86
+ });
87
+ }
88
+ // Add handling for other structural elements as needed
89
+ });
90
+ };
91
+ collectTextFromContent(res.data.body.content);
92
+ // Sort segments by starting position to ensure correct ordering
93
+ segments.sort((a, b) => a.start - b.start);
94
+ console.log(`Document ${documentId} contains ${segments.length} text segments and ${fullText.length} characters in total.`);
95
+ // Find the specified instance of the text
96
+ let startIndex = -1;
97
+ let endIndex = -1;
98
+ let foundCount = 0;
99
+ let searchStartIndex = 0;
100
+ while (foundCount < instance) {
101
+ const currentIndex = fullText.indexOf(textToFind, searchStartIndex);
102
+ if (currentIndex === -1) {
103
+ console.log(`Search text "${textToFind}" not found for instance ${foundCount + 1} (requested: ${instance})`);
104
+ break;
105
+ }
106
+ foundCount++;
107
+ console.log(`Found instance ${foundCount} of "${textToFind}" at position ${currentIndex} in full text`);
108
+ if (foundCount === instance) {
109
+ const targetStartInFullText = currentIndex;
110
+ const targetEndInFullText = currentIndex + textToFind.length;
111
+ let currentPosInFullText = 0;
112
+ console.log(`Target text range in full text: ${targetStartInFullText}-${targetEndInFullText}`);
113
+ for (const seg of segments) {
114
+ const segStartInFullText = currentPosInFullText;
115
+ const segTextLength = seg.text.length;
116
+ const segEndInFullText = segStartInFullText + segTextLength;
117
+ // Map from reconstructed text position to actual document indices
118
+ if (startIndex === -1 && targetStartInFullText >= segStartInFullText && targetStartInFullText < segEndInFullText) {
119
+ startIndex = seg.start + (targetStartInFullText - segStartInFullText);
120
+ console.log(`Mapped start to segment ${seg.start}-${seg.end}, position ${startIndex}`);
121
+ }
122
+ if (targetEndInFullText > segStartInFullText && targetEndInFullText <= segEndInFullText) {
123
+ endIndex = seg.start + (targetEndInFullText - segStartInFullText);
124
+ console.log(`Mapped end to segment ${seg.start}-${seg.end}, position ${endIndex}`);
125
+ break;
126
+ }
127
+ currentPosInFullText = segEndInFullText;
128
+ }
129
+ if (startIndex === -1 || endIndex === -1) {
130
+ console.warn(`Failed to map text "${textToFind}" instance ${instance} to actual document indices`);
131
+ // Reset and try next occurrence
132
+ startIndex = -1;
133
+ endIndex = -1;
134
+ searchStartIndex = currentIndex + 1;
135
+ foundCount--;
136
+ continue;
137
+ }
138
+ console.log(`Successfully mapped "${textToFind}" to document range ${startIndex}-${endIndex}`);
139
+ return { startIndex, endIndex };
140
+ }
141
+ // Prepare for next search iteration
142
+ searchStartIndex = currentIndex + 1;
143
+ }
144
+ console.warn(`Could not find instance ${instance} of text "${textToFind}" in document ${documentId}`);
145
+ return null; // Instance not found or mapping failed for all attempts
146
+ }
147
+ catch (error) {
148
+ console.error(`Error finding text "${textToFind}" in doc ${documentId}: ${error.message || 'Unknown error'}`);
149
+ if (error.code === 404)
150
+ throw new UserError(`Document not found while searching text (ID: ${documentId}).`);
151
+ if (error.code === 403)
152
+ throw new UserError(`Permission denied while searching text in doc ${documentId}.`);
153
+ throw new Error(`Failed to retrieve doc for text searching: ${error.message || 'Unknown error'}`);
154
+ }
155
+ }
156
+ // --- Paragraph Boundary Helper ---
157
+ // Enhanced version to handle document structural elements more robustly
158
+ export async function getParagraphRange(docs, documentId, indexWithin) {
159
+ try {
160
+ console.log(`Finding paragraph containing index ${indexWithin} in document ${documentId}`);
161
+ // Request more detailed document structure to handle nested elements
162
+ const res = await docs.documents.get({
163
+ documentId,
164
+ // Request more comprehensive structure information
165
+ fields: 'body(content(startIndex,endIndex,paragraph,table,sectionBreak,tableOfContents))',
166
+ });
167
+ if (!res.data.body?.content) {
168
+ console.warn(`No content found in document ${documentId}`);
169
+ return null;
170
+ }
171
+ // Find paragraph containing the index
172
+ // We'll look at all structural elements recursively
173
+ const findParagraphInContent = (content) => {
174
+ for (const element of content) {
175
+ // Check if we have element boundaries defined
176
+ if (element.startIndex !== undefined && element.endIndex !== undefined) {
177
+ // Check if index is within this element's range first
178
+ if (indexWithin >= element.startIndex && indexWithin < element.endIndex) {
179
+ // If it's a paragraph, we've found our target
180
+ if (element.paragraph) {
181
+ console.log(`Found paragraph containing index ${indexWithin}, range: ${element.startIndex}-${element.endIndex}`);
182
+ return {
183
+ startIndex: element.startIndex,
184
+ endIndex: element.endIndex
185
+ };
186
+ }
187
+ // If it's a table, we need to check cells recursively
188
+ if (element.table && element.table.tableRows) {
189
+ console.log(`Index ${indexWithin} is within a table, searching cells...`);
190
+ for (const row of element.table.tableRows) {
191
+ if (row.tableCells) {
192
+ for (const cell of row.tableCells) {
193
+ if (cell.content) {
194
+ const result = findParagraphInContent(cell.content);
195
+ if (result)
196
+ return result;
197
+ }
198
+ }
199
+ }
200
+ }
201
+ }
202
+ // For other structural elements, we didn't find a paragraph
203
+ // but we know the index is within this element
204
+ console.warn(`Index ${indexWithin} is within element (${element.startIndex}-${element.endIndex}) but not in a paragraph`);
205
+ }
206
+ }
207
+ }
208
+ return null;
209
+ };
210
+ const paragraphRange = findParagraphInContent(res.data.body.content);
211
+ if (!paragraphRange) {
212
+ console.warn(`Could not find paragraph containing index ${indexWithin}`);
213
+ }
214
+ else {
215
+ console.log(`Returning paragraph range: ${paragraphRange.startIndex}-${paragraphRange.endIndex}`);
216
+ }
217
+ return paragraphRange;
218
+ }
219
+ catch (error) {
220
+ console.error(`Error getting paragraph range for index ${indexWithin} in doc ${documentId}: ${error.message || 'Unknown error'}`);
221
+ if (error.code === 404)
222
+ throw new UserError(`Document not found while finding paragraph (ID: ${documentId}).`);
223
+ if (error.code === 403)
224
+ throw new UserError(`Permission denied while accessing doc ${documentId}.`);
225
+ throw new Error(`Failed to find paragraph: ${error.message || 'Unknown error'}`);
226
+ }
227
+ }
228
+ // --- Style Request Builders ---
229
+ export function buildUpdateTextStyleRequest(startIndex, endIndex, style) {
230
+ const textStyle = {};
231
+ const fieldsToUpdate = [];
232
+ if (style.bold !== undefined) {
233
+ textStyle.bold = style.bold;
234
+ fieldsToUpdate.push('bold');
235
+ }
236
+ if (style.italic !== undefined) {
237
+ textStyle.italic = style.italic;
238
+ fieldsToUpdate.push('italic');
239
+ }
240
+ if (style.underline !== undefined) {
241
+ textStyle.underline = style.underline;
242
+ fieldsToUpdate.push('underline');
243
+ }
244
+ if (style.strikethrough !== undefined) {
245
+ textStyle.strikethrough = style.strikethrough;
246
+ fieldsToUpdate.push('strikethrough');
247
+ }
248
+ if (style.fontSize !== undefined) {
249
+ textStyle.fontSize = { magnitude: style.fontSize, unit: 'PT' };
250
+ fieldsToUpdate.push('fontSize');
251
+ }
252
+ if (style.fontFamily !== undefined) {
253
+ textStyle.weightedFontFamily = { fontFamily: style.fontFamily };
254
+ fieldsToUpdate.push('weightedFontFamily');
255
+ }
256
+ if (style.foregroundColor !== undefined) {
257
+ const rgbColor = hexToRgbColor(style.foregroundColor);
258
+ if (!rgbColor)
259
+ throw new UserError(`Invalid foreground hex color format: ${style.foregroundColor}`);
260
+ textStyle.foregroundColor = { color: { rgbColor: rgbColor } };
261
+ fieldsToUpdate.push('foregroundColor');
262
+ }
263
+ if (style.backgroundColor !== undefined) {
264
+ const rgbColor = hexToRgbColor(style.backgroundColor);
265
+ if (!rgbColor)
266
+ throw new UserError(`Invalid background hex color format: ${style.backgroundColor}`);
267
+ textStyle.backgroundColor = { color: { rgbColor: rgbColor } };
268
+ fieldsToUpdate.push('backgroundColor');
269
+ }
270
+ if (style.linkUrl !== undefined) {
271
+ textStyle.link = { url: style.linkUrl };
272
+ fieldsToUpdate.push('link');
273
+ }
274
+ // TODO: Handle clearing formatting
275
+ if (fieldsToUpdate.length === 0)
276
+ return null; // No styles to apply
277
+ const request = {
278
+ updateTextStyle: {
279
+ range: { startIndex, endIndex },
280
+ textStyle: textStyle,
281
+ fields: fieldsToUpdate.join(','),
282
+ }
283
+ };
284
+ return { request, fields: fieldsToUpdate };
285
+ }
286
+ export function buildUpdateParagraphStyleRequest(startIndex, endIndex, style) {
287
+ // Create style object and track which fields to update
288
+ const paragraphStyle = {};
289
+ const fieldsToUpdate = [];
290
+ console.log(`Building paragraph style request for range ${startIndex}-${endIndex} with options:`, style);
291
+ // Process alignment option (LEFT, CENTER, RIGHT, JUSTIFIED)
292
+ if (style.alignment !== undefined) {
293
+ paragraphStyle.alignment = style.alignment;
294
+ fieldsToUpdate.push('alignment');
295
+ console.log(`Setting alignment to ${style.alignment}`);
296
+ }
297
+ // Process indentation options
298
+ if (style.indentStart !== undefined) {
299
+ paragraphStyle.indentStart = { magnitude: style.indentStart, unit: 'PT' };
300
+ fieldsToUpdate.push('indentStart');
301
+ console.log(`Setting left indent to ${style.indentStart}pt`);
302
+ }
303
+ if (style.indentEnd !== undefined) {
304
+ paragraphStyle.indentEnd = { magnitude: style.indentEnd, unit: 'PT' };
305
+ fieldsToUpdate.push('indentEnd');
306
+ console.log(`Setting right indent to ${style.indentEnd}pt`);
307
+ }
308
+ // Process spacing options
309
+ if (style.spaceAbove !== undefined) {
310
+ paragraphStyle.spaceAbove = { magnitude: style.spaceAbove, unit: 'PT' };
311
+ fieldsToUpdate.push('spaceAbove');
312
+ console.log(`Setting space above to ${style.spaceAbove}pt`);
313
+ }
314
+ if (style.spaceBelow !== undefined) {
315
+ paragraphStyle.spaceBelow = { magnitude: style.spaceBelow, unit: 'PT' };
316
+ fieldsToUpdate.push('spaceBelow');
317
+ console.log(`Setting space below to ${style.spaceBelow}pt`);
318
+ }
319
+ // Process named style types (headings, etc.)
320
+ if (style.namedStyleType !== undefined) {
321
+ paragraphStyle.namedStyleType = style.namedStyleType;
322
+ fieldsToUpdate.push('namedStyleType');
323
+ console.log(`Setting named style to ${style.namedStyleType}`);
324
+ }
325
+ // Process page break control
326
+ if (style.keepWithNext !== undefined) {
327
+ paragraphStyle.keepWithNext = style.keepWithNext;
328
+ fieldsToUpdate.push('keepWithNext');
329
+ console.log(`Setting keepWithNext to ${style.keepWithNext}`);
330
+ }
331
+ // Verify we have styles to apply
332
+ if (fieldsToUpdate.length === 0) {
333
+ console.warn("No paragraph styling options were provided");
334
+ return null; // No styles to apply
335
+ }
336
+ // Build the request object
337
+ const request = {
338
+ updateParagraphStyle: {
339
+ range: { startIndex, endIndex },
340
+ paragraphStyle: paragraphStyle,
341
+ fields: fieldsToUpdate.join(','),
342
+ }
343
+ };
344
+ console.log(`Created paragraph style request with fields: ${fieldsToUpdate.join(', ')}`);
345
+ return { request, fields: fieldsToUpdate };
346
+ }
347
+ // --- Specific Feature Helpers ---
348
+ export async function createTable(docs, documentId, rows, columns, index) {
349
+ if (rows < 1 || columns < 1) {
350
+ throw new UserError("Table must have at least 1 row and 1 column.");
351
+ }
352
+ const request = {
353
+ insertTable: {
354
+ location: { index },
355
+ rows: rows,
356
+ columns: columns,
357
+ }
358
+ };
359
+ return executeBatchUpdate(docs, documentId, [request]);
360
+ }
361
+ export async function insertText(docs, documentId, text, index) {
362
+ if (!text)
363
+ return {}; // Nothing to insert
364
+ const request = {
365
+ insertText: {
366
+ location: { index },
367
+ text: text,
368
+ }
369
+ };
370
+ return executeBatchUpdate(docs, documentId, [request]);
371
+ }
372
+ // --- Complex / Stubbed Helpers ---
373
+ export async function findParagraphsMatchingStyle(docs, documentId, styleCriteria // Define a proper type for criteria (e.g., { fontFamily: 'Arial', bold: true })
374
+ ) {
375
+ // TODO: Implement logic
376
+ // 1. Get document content with paragraph elements and their styles.
377
+ // 2. Iterate through paragraphs.
378
+ // 3. For each paragraph, check if its computed style matches the criteria.
379
+ // 4. Return ranges of matching paragraphs.
380
+ console.warn("findParagraphsMatchingStyle is not implemented.");
381
+ throw new NotImplementedError("Finding paragraphs by style criteria is not yet implemented.");
382
+ // return [];
383
+ }
384
+ export async function detectAndFormatLists(docs, documentId, startIndex, endIndex) {
385
+ // TODO: Implement complex logic
386
+ // 1. Get document content (paragraphs, text runs) in the specified range (or whole doc).
387
+ // 2. Iterate through paragraphs.
388
+ // 3. Identify sequences of paragraphs starting with list-like markers (e.g., "-", "*", "1.", "a)").
389
+ // 4. Determine nesting levels based on indentation or marker patterns.
390
+ // 5. Generate CreateParagraphBulletsRequests for the identified sequences.
391
+ // 6. Potentially delete the original marker text.
392
+ // 7. Execute the batch update.
393
+ console.warn("detectAndFormatLists is not implemented.");
394
+ throw new NotImplementedError("Automatic list detection and formatting is not yet implemented.");
395
+ // return {};
396
+ }
397
+ export async function addCommentHelper(docs, documentId, text, startIndex, endIndex) {
398
+ // NOTE: Adding comments typically requires the Google Drive API v3 and different scopes!
399
+ // 'https://www.googleapis.com/auth/drive' or more specific comment scopes.
400
+ // This helper is a placeholder assuming Drive API client (`drive`) is available and authorized.
401
+ /*
402
+ const drive = google.drive({version: 'v3', auth: authClient}); // Assuming authClient is available
403
+ await drive.comments.create({
404
+ fileId: documentId,
405
+ requestBody: {
406
+ content: text,
407
+ anchor: JSON.stringify({ // Anchor format might need verification
408
+ 'type': 'workbook#textAnchor', // Or appropriate type for Docs
409
+ 'refs': [{
410
+ 'docRevisionId': 'head', // Or specific revision
411
+ 'range': {
412
+ 'start': startIndex,
413
+ 'end': endIndex,
414
+ }
415
+ }]
416
+ })
417
+ },
418
+ fields: 'id'
419
+ });
420
+ */
421
+ console.warn("addCommentHelper requires Google Drive API and is not implemented.");
422
+ throw new NotImplementedError("Adding comments requires Drive API setup and is not yet implemented.");
423
+ }
424
+ // --- Image Insertion Helpers ---
425
+ /**
426
+ * Inserts an inline image into a document from a publicly accessible URL
427
+ * @param docs - Google Docs API client
428
+ * @param documentId - The document ID
429
+ * @param imageUrl - Publicly accessible URL to the image
430
+ * @param index - Position in the document where image should be inserted (1-based)
431
+ * @param width - Optional width in points
432
+ * @param height - Optional height in points
433
+ * @returns Promise with batch update response
434
+ */
435
+ export async function insertInlineImage(docs, documentId, imageUrl, index, width, height) {
436
+ // Validate URL format
437
+ try {
438
+ new URL(imageUrl);
439
+ }
440
+ catch (e) {
441
+ throw new UserError(`Invalid image URL format: ${imageUrl}`);
442
+ }
443
+ // Build the insertInlineImage request
444
+ const request = {
445
+ insertInlineImage: {
446
+ location: { index },
447
+ uri: imageUrl,
448
+ ...(width && height && {
449
+ objectSize: {
450
+ height: { magnitude: height, unit: 'PT' },
451
+ width: { magnitude: width, unit: 'PT' }
452
+ }
453
+ })
454
+ }
455
+ };
456
+ return executeBatchUpdate(docs, documentId, [request]);
457
+ }
458
+ /**
459
+ * Uploads a local image file to Google Drive and returns its public URL
460
+ * @param drive - Google Drive API client
461
+ * @param localFilePath - Path to the local image file
462
+ * @param parentFolderId - Optional parent folder ID (defaults to root)
463
+ * @returns Promise with the public webContentLink URL
464
+ */
465
+ export async function uploadImageToDrive(drive, // drive_v3.Drive type
466
+ localFilePath, parentFolderId) {
467
+ const fs = await import('fs');
468
+ const path = await import('path');
469
+ // Verify file exists
470
+ if (!fs.existsSync(localFilePath)) {
471
+ throw new UserError(`Image file not found: ${localFilePath}`);
472
+ }
473
+ // Get file name and mime type
474
+ const fileName = path.basename(localFilePath);
475
+ const mimeTypeMap = {
476
+ '.jpg': 'image/jpeg',
477
+ '.jpeg': 'image/jpeg',
478
+ '.png': 'image/png',
479
+ '.gif': 'image/gif',
480
+ '.bmp': 'image/bmp',
481
+ '.webp': 'image/webp',
482
+ '.svg': 'image/svg+xml'
483
+ };
484
+ const ext = path.extname(localFilePath).toLowerCase();
485
+ const mimeType = mimeTypeMap[ext] || 'application/octet-stream';
486
+ // Upload file to Drive
487
+ const fileMetadata = {
488
+ name: fileName,
489
+ mimeType: mimeType
490
+ };
491
+ if (parentFolderId) {
492
+ fileMetadata.parents = [parentFolderId];
493
+ }
494
+ const media = {
495
+ mimeType: mimeType,
496
+ body: fs.createReadStream(localFilePath)
497
+ };
498
+ const uploadResponse = await drive.files.create({
499
+ requestBody: fileMetadata,
500
+ media: media,
501
+ fields: 'id,webViewLink,webContentLink'
502
+ });
503
+ const fileId = uploadResponse.data.id;
504
+ if (!fileId) {
505
+ throw new Error('Failed to upload image to Drive - no file ID returned');
506
+ }
507
+ // Make the file publicly readable
508
+ await drive.permissions.create({
509
+ fileId: fileId,
510
+ supportsAllDrives: true,
511
+ requestBody: {
512
+ role: 'reader',
513
+ type: 'anyone'
514
+ }
515
+ });
516
+ // Get the webContentLink
517
+ const fileInfo = await drive.files.get({
518
+ fileId: fileId,
519
+ fields: 'webContentLink'
520
+ });
521
+ const webContentLink = fileInfo.data.webContentLink;
522
+ if (!webContentLink) {
523
+ throw new Error('Failed to get public URL for uploaded image');
524
+ }
525
+ return webContentLink;
526
+ }
527
+ /**
528
+ * Recursively collect all tabs from a document in a flat list with hierarchy info
529
+ * @param doc - The Google Doc document object
530
+ * @returns Array of tabs with nesting level information
531
+ */
532
+ export function getAllTabs(doc) {
533
+ const allTabs = [];
534
+ if (!doc.tabs || doc.tabs.length === 0) {
535
+ return allTabs;
536
+ }
537
+ for (const tab of doc.tabs) {
538
+ addCurrentAndChildTabs(tab, allTabs, 0);
539
+ }
540
+ return allTabs;
541
+ }
542
+ /**
543
+ * Recursive helper to add tabs with their nesting level
544
+ * @param tab - The tab to add
545
+ * @param allTabs - The accumulator array
546
+ * @param level - Current nesting level (0 for top-level)
547
+ */
548
+ function addCurrentAndChildTabs(tab, allTabs, level) {
549
+ allTabs.push({ ...tab, level });
550
+ if (tab.childTabs && tab.childTabs.length > 0) {
551
+ for (const childTab of tab.childTabs) {
552
+ addCurrentAndChildTabs(childTab, allTabs, level + 1);
553
+ }
554
+ }
555
+ }
556
+ /**
557
+ * Get the text length from a DocumentTab
558
+ * @param documentTab - The DocumentTab object
559
+ * @returns Total character count
560
+ */
561
+ export function getTabTextLength(documentTab) {
562
+ let totalLength = 0;
563
+ if (!documentTab?.body?.content) {
564
+ return 0;
565
+ }
566
+ documentTab.body.content.forEach((element) => {
567
+ // Handle paragraphs
568
+ if (element.paragraph?.elements) {
569
+ element.paragraph.elements.forEach((pe) => {
570
+ if (pe.textRun?.content) {
571
+ totalLength += pe.textRun.content.length;
572
+ }
573
+ });
574
+ }
575
+ // Handle tables
576
+ if (element.table?.tableRows) {
577
+ element.table.tableRows.forEach((row) => {
578
+ row.tableCells?.forEach((cell) => {
579
+ cell.content?.forEach((cellElement) => {
580
+ cellElement.paragraph?.elements?.forEach((pe) => {
581
+ if (pe.textRun?.content) {
582
+ totalLength += pe.textRun.content.length;
583
+ }
584
+ });
585
+ });
586
+ });
587
+ });
588
+ }
589
+ });
590
+ return totalLength;
591
+ }
592
+ /**
593
+ * Find a specific tab by ID in a document (searches recursively through child tabs)
594
+ * @param doc - The Google Doc document object
595
+ * @param tabId - The tab ID to search for
596
+ * @returns The tab object if found, null otherwise
597
+ */
598
+ export function findTabById(doc, tabId) {
599
+ if (!doc.tabs || doc.tabs.length === 0) {
600
+ return null;
601
+ }
602
+ // Helper function to search through tabs recursively
603
+ const searchTabs = (tabs) => {
604
+ for (const tab of tabs) {
605
+ if (tab.tabProperties?.tabId === tabId) {
606
+ return tab;
607
+ }
608
+ // Recursively search child tabs
609
+ if (tab.childTabs && tab.childTabs.length > 0) {
610
+ const found = searchTabs(tab.childTabs);
611
+ if (found)
612
+ return found;
613
+ }
614
+ }
615
+ return null;
616
+ };
617
+ return searchTabs(doc.tabs);
618
+ }