@adobe/helix-google-support 2.2.17 → 2.3.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [2.3.0](https://github.com/adobe/helix-google-support/compare/v2.2.17...v2.3.0) (2023-08-31)
2
+
3
+
4
+ ### Features
5
+
6
+ * sheet create/update/delete functionalities ([a6b8e65](https://github.com/adobe/helix-google-support/commit/a6b8e65280e964201e6a3be2e24a681a3e3cd2f8))
7
+
1
8
  ## [2.2.17](https://github.com/adobe/helix-google-support/compare/v2.2.16...v2.2.17) (2023-08-28)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-google-support",
3
- "version": "2.2.17",
3
+ "version": "2.3.0",
4
4
  "description": "Helix Google Support",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -156,4 +156,35 @@ declare class GoogleClient {
156
156
  * @returns {Promise<object>|null} The document or {@code null} if the document does not exist
157
157
  */
158
158
  getDocumentFromPath(parentId:string, path:string, noRetry:boolean):Promise<object>;
159
+
160
+ /**
161
+ * create a new file in the given parent folder with the given name and mimetype
162
+ * @param {string} parentId
163
+ * @param {string} name
164
+ * @param {string} mimeType either {@link GoogleClient.TYPE_DOCUMENT} or {@link GoogleClient.TYPE_SPREADSHEET}
165
+ * @returns {Promise<object>} file object
166
+ */
167
+ createBlankDocOrSheet(parentId:string, name:string, mimeType:string):Promise<object>;
168
+
169
+
170
+ /**
171
+ *
172
+ * @param {string} spreadsheetId
173
+ * @param {string} sheetName
174
+ * @param {object} worksheetData
175
+ *
176
+ * @returns {Promise<string>} sheetId or {@code null}
177
+ */
178
+ updateSheet(spreadsheetId:string, sheetName:string, worksheetData:object, create:boolean):Promise<string>;
179
+
180
+
181
+ /**
182
+ *
183
+ * @param spreadsheetId
184
+ * @param sheetName sheetName to delete in the spreadsheet
185
+ *
186
+ * @returns {Promise<void>}
187
+ */
188
+ deleteSheet(spreadsheetId:string, sheetName:string):Promise<void>;
189
+
159
190
  }
@@ -562,6 +562,153 @@ export class GoogleClient {
562
562
  }
563
563
  }
564
564
  }
565
+
566
+ /**
567
+ *
568
+ * @param {string} parentId
569
+ * @param {string} name
570
+ * @param {string} mimeType one of GoogleClient.TYPE_DOCUMENT or GoogleClient.TYPE_SPREADSHEET
571
+ * @returns {Promise<object>} file object
572
+ */
573
+ async createBlankDocOrSheet(parentId, name, mimeType) {
574
+ try {
575
+ if (
576
+ mimeType !== GoogleClient.TYPE_DOCUMENT
577
+ && mimeType !== GoogleClient.TYPE_SPREADSHEET
578
+ ) {
579
+ throw new Error(`Invalid mimeType ${mimeType}`);
580
+ }
581
+ const requestBody = {
582
+ name,
583
+ mimeType,
584
+ };
585
+
586
+ if (parentId) {
587
+ requestBody.parents = [parentId];
588
+ }
589
+
590
+ const response = await this.drive.files.create({
591
+ requestBody,
592
+ });
593
+
594
+ return response.data;
595
+ } catch (e) {
596
+ this.log.info(`Error creating file ${name}`);
597
+ throw e;
598
+ }
599
+ }
600
+
601
+ /**
602
+ *
603
+ * @param {string} spreadsheetId
604
+ * @param {string} sheetName
605
+ * @param {object} worksheetData
606
+ * @param {boolean} create - Indicates whether to create the sheet if it doesn't exist
607
+ * @returns {Promise<string>} sheetId or {@code null}
608
+ */
609
+ async updateSheet(spreadsheetId, sheetName, worksheetData, create = true) {
610
+ try {
611
+ const sheets = google.sheets({
612
+ version: 'v4',
613
+ auth: this.auth,
614
+ });
615
+
616
+ // Check if the sheet already exists
617
+ const spreadsheetInfo = await sheets.spreadsheets.get({ spreadsheetId });
618
+
619
+ const sheetExists = spreadsheetInfo.data.sheets.some(
620
+ (sheet) => sheet.properties.title === sheetName,
621
+ );
622
+
623
+ let sheetId;
624
+
625
+ if (!sheetExists) {
626
+ // Create a new sheet if it doesn't exist and the 'create' flag is true
627
+ if (!create) {
628
+ this.log.info('Sheet does not exists and not creating, nothing to do.');
629
+ return null;
630
+ }
631
+
632
+ const createResponse = await sheets.spreadsheets.batchUpdate({
633
+ spreadsheetId,
634
+ requestBody: {
635
+ requests: [
636
+ {
637
+ addSheet: {
638
+ properties: {
639
+ title: sheetName,
640
+ },
641
+ },
642
+ },
643
+ ],
644
+ },
645
+ });
646
+
647
+ // Get the sheet ID of the newly created sheet
648
+ sheetId = createResponse.data.replies[0].addSheet.properties.sheetId;
649
+ } else {
650
+ // Get the sheet ID of the existing sheet
651
+ const sheetInfo = spreadsheetInfo.data.sheets.find(
652
+ (sheet) => sheet.properties.title === sheetName,
653
+ );
654
+ sheetId = sheetInfo.properties.sheetId;
655
+ }
656
+
657
+ if (Array.isArray(worksheetData) && Array.isArray(worksheetData[0])) {
658
+ await sheets.spreadsheets.values.update({
659
+ spreadsheetId,
660
+ range: `'${sheetName}'`,
661
+ valueInputOption: 'RAW',
662
+ resource: {
663
+ values: worksheetData,
664
+ },
665
+ });
666
+ }
667
+ return sheetId;
668
+ } catch (error) {
669
+ this.log.info(`Error in updating sheet: ${error.message}`);
670
+ throw error;
671
+ }
672
+ }
673
+
674
+ /**
675
+ *
676
+ * @param {string} spreadsheetId
677
+ * @param {string} sheetName
678
+ *
679
+ * @returns void
680
+ */
681
+ async deleteGSheet(spreadsheetId, sheetName) {
682
+ try {
683
+ const sheets = google.sheets({
684
+ version: 'v4',
685
+ auth: this.auth,
686
+ });
687
+
688
+ const sheetsResponse = await sheets.spreadsheets.get({
689
+ spreadsheetId,
690
+ ranges: [sheetName],
691
+ fields: 'sheets.properties.sheetId',
692
+ });
693
+
694
+ const existingSheetId = sheetsResponse.data.sheets[0].properties.sheetId;
695
+ await sheets.spreadsheets.batchUpdate({
696
+ spreadsheetId,
697
+ requestBody: {
698
+ requests: [
699
+ {
700
+ deleteSheet: {
701
+ sheetId: existingSheetId,
702
+ },
703
+ },
704
+ ],
705
+ },
706
+ });
707
+ } catch (error) {
708
+ this.log.info(`Error deleting sheet from spreadsheet: ${error.message}`);
709
+ throw error;
710
+ }
711
+ }
565
712
  }
566
713
 
567
714
  Object.assign(GoogleClient, {