From f207f424855f32aee618d357e1f98d6bd4b6d088 Mon Sep 17 00:00:00 2001 From: Justin Greywolf Date: Wed, 29 Nov 2023 19:27:58 -0800 Subject: [PATCH] Rework powerpoint refresh function --- .../lib/client/clientApi/PowerPointService.ts | 38 +++++++++---------- .../src/lib/client/clientApi/WordService.ts | 18 ++++----- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/packages/office/src/lib/client/clientApi/PowerPointService.ts b/packages/office/src/lib/client/clientApi/PowerPointService.ts index 2384677..15bbf94 100644 --- a/packages/office/src/lib/client/clientApi/PowerPointService.ts +++ b/packages/office/src/lib/client/clientApi/PowerPointService.ts @@ -36,34 +36,30 @@ export class PowerPointService extends OfficeService { } public async syncDiagrams(): Promise { - OfficeExtension.config.extendedErrorLogging = true; await PowerPoint.run(async (context) => { - const presentation = context.presentation; - const slides = presentation.slides.load("items"); - - await context.sync(); - - for(let slideIndex = 0; slideIndex < slides.items.length; slideIndex++) {`` - const slide = slides.getItemAt(slideIndex); - const shapes = slide.shapes.load('items'); + const slides = context.presentation.slides.load("items"); await context.sync(); - - for (let shapeIndex = 0; shapeIndex < shapes.items.length; shapeIndex++) { - const shape = shapes.getItemAt(shapeIndex).load('tags/items'); + + for (let i = 0; i < slides.items.length; i++) { + const currentSlide = slides.items[i]; + const shapes = currentSlide.shapes.load("items"); await context.sync(); - - try{ - const diagramTag = shape.tags.getItem(C.TokenSettingName.toUpperCase()).load('key, value');; + + for (let shapeIndex = 0; shapeIndex < shapes.items.length; shapeIndex++) { + const currentShape = shapes.items[shapeIndex]; + const tags = currentShape.tags.load("key, value"); await context.sync(); - if(diagramTag) { - shape.delete(); - await this.replaceExistingDiagram(diagramTag.value); + + for (let tagIndex = 0; tagIndex < tags.items.length; tagIndex++) { + const currentTag = tags.items[tagIndex]; + + if (currentTag.key === C.TokenSettingName.toUpperCase()) { + currentShape.delete(); + await this.replaceExistingDiagram(currentTag.value); + } } - } catch (error) { - throw new RefreshError(`Error encountered while updating diagrams:`, error as Error); } } - } }); } diff --git a/packages/office/src/lib/client/clientApi/WordService.ts b/packages/office/src/lib/client/clientApi/WordService.ts index 2f7f5ae..9fdf061 100644 --- a/packages/office/src/lib/client/clientApi/WordService.ts +++ b/packages/office/src/lib/client/clientApi/WordService.ts @@ -54,7 +54,7 @@ export class WordService extends OfficeService { } public async syncDiagrams() { - const contentControlsToUpdate = await this.getContentControls().catch((error) => { + const controlTagsToUpdate = await this.getContentControlTagsToUpdate().catch((error) => { if(error instanceof ContentControlsNotFoundError) { showUserMessage( 'No diagrams found in document', @@ -63,10 +63,10 @@ export class WordService extends OfficeService { } }); - if(contentControlsToUpdate) { + if(controlTagsToUpdate) { try { - for (const control of contentControlsToUpdate) { - await this.replaceExistingDiagram(control); + for (const tag of controlTagsToUpdate) { + await this.replaceExistingDiagram(tag); } } catch (error) { if(error instanceof RefreshError) { @@ -81,8 +81,8 @@ export class WordService extends OfficeService { } } - private async getContentControls() { - const contentControlsList: string[] = []; + private async getContentControlTagsToUpdate() { + const tagList: string[] = []; try { await Word.run(async (context) => { @@ -93,7 +93,7 @@ export class WordService extends OfficeService { for (let i = 0; i < contentControls.items.length; i++) { const tag = contentControls.items[i].tag; if(tag.startsWith(C.TokenSettingName)) { - contentControlsList.push(contentControls.items[i].tag); + tagList.push(contentControls.items[i].tag); } } }); @@ -102,11 +102,11 @@ export class WordService extends OfficeService { throw new Error('unknown error getting content controls'); } - if(contentControlsList.length === 0) { + if(tagList.length === 0) { throw new ContentControlsNotFoundError(); } - return contentControlsList; + return tagList; } private async replaceExistingDiagram(tag: string) {