Merge files

Posted by: pedro.moraes on 5 December 2024, 9:26 am EST

    • Post Options:
    • Link

    Posted 5 December 2024, 9:26 am EST

    Hello everyone,

    Some time ago I merged the original file with an imported file as you taught

    However, for very large files using “toJSON” and “fromJSON” takes a long time.

    I would like to know if there is a better way to merge these files?

    ...
    const arquivoMesclado = this.mesclarService.mesclar(
            this.workbookAuxiliar,
            this.workbookOficial,
            this.opcoesExcel,
            this.paginasSobrescritas
          );
          this.workbookOficial.fromJSON(arquivoMesclado, {
            frozenRowsAsColumnHeaders: this.opcoesExcel.frozenLinhaHeader,
            frozenColumnsAsRowHeaders: this.opcoesExcel.frozenColunaHeader,
            doNotRecalculateAfterLoad: false,
          });
    ...
    public mesclar(
        workbookNovo: GC.Spread.Sheets.Workbook,
        workbookEmUso: GC.Spread.Sheets.Workbook,
        opcoes: { temFormula: boolean; temEstilo: boolean },
        paginasSobrescritas: string[] = []
      ): any {
        this.removerRestricoesDeBloqueioParaNovasPaginas(workbookNovo);
        this.inicializar(workbookNovo, workbookEmUso, opcoes);
        this.atribuirNomesDasPaginasExistentes();
        this.mapearEstilosImportados();
        this.atualizarJsonComNovasPaginas(paginasSobrescritas);
        return this.jsonEmUso;
      }
    
      private inicializar(
        workbookNovo: GC.Spread.Sheets.Workbook,
        workbookEmUso: GC.Spread.Sheets.Workbook,
        opcoes: { temFormula: boolean; temEstilo: boolean }
      ): void {
        this.jsonEmUso = workbookEmUso.toJSON();
        this.jsonNovo = workbookNovo.toJSON();
        this.opcoes = opcoes;
        this.nomeDasPaginas = new Set();
        this.mapaEstiloDoArquivoImportado = new Map();
        this.verificarEIniciarEstruturas();
      }
    
      private verificarEIniciarEstruturas(): void {
        this.jsonEmUso.namedStyles ??= [];
        this.jsonEmUso.externalReference ??= [];
      }
    
      private removerRestricoesDeBloqueioParaNovasPaginas(workbookNovo: GC.Spread.Sheets.Workbook): void {
        const estiloDesbloqueado = new GC.Spread.Sheets.Style();
        estiloDesbloqueado.locked = false;
        workbookNovo.sheets.forEach((sheet) => {
          sheet.setDefaultStyle(estiloDesbloqueado, GC.Spread.Sheets.SheetArea.viewport);
        });
      }
    
      private atribuirNomesDasPaginasExistentes(): void {
        Object.keys(this.jsonEmUso.sheets).forEach((nome) => {
          this.nomeDasPaginas.add(nome);
        });
      }
    
      private mapearEstilosImportados(): void {
        if (!this.opcoes.temEstilo || !this.jsonNovo.namedStyles) {
          this.jsonNovo.namedStyles = [];
          return;
        }
    
        const timestamp = Date.now();
        this.jsonNovo.namedStyles.forEach((estilo, index) => {
          const novoNome = `__estilo${index + 1}_${timestamp}`;
          this.mapaEstiloDoArquivoImportado.set(estilo.name, novoNome);
          estilo.name = novoNome;
          this.jsonEmUso.namedStyles.push(estilo);
        });
      }
    
      private atualizarJsonComNovasPaginas(paginasSobrescritas: string[]): void {
        Object.entries(this.jsonNovo.sheets).forEach(([nomePagina, jsonPagina]) => {
          paginasSobrescritas.includes(nomePagina)
            ? this.sobrescreverPagina(nomePagina, jsonPagina)
            : this.adicionarPagina(nomePagina, jsonPagina);
        });
      }
    
      private adicionarPagina(nomePagina: string, jsonPagina: any): void {
        const novoNome = this.gerarNomeUnico(nomePagina);
        jsonPagina.isSelected = false;
        jsonPagina.names = [];
        jsonPagina.name = novoNome;
        jsonPagina.index = this.nomeDasPaginas.size;
    
        this.aplicarEstiloEFormula(jsonPagina);
        this.nomeDasPaginas.add(novoNome);
        this.jsonEmUso.sheets[novoNome] = jsonPagina;
        this.jsonEmUso.sheetCount = this.nomeDasPaginas.size;
      }
    
      private sobrescreverPagina(nomePagina: string, jsonPagina: any): void {
        const index = this.jsonEmUso.sheets[nomePagina].index;
        jsonPagina.isSelected = false;
        jsonPagina.names = [];
        jsonPagina.index = index;
    
        this.aplicarEstiloEFormula(jsonPagina);
        this.jsonEmUso.sheets[nomePagina] = jsonPagina;
      }
    
      private gerarNomeUnico(nomeBase: string, separador = '_'): string {
        let contador = 0;
        let nome = nomeBase;
    
        while (this.nomeDasPaginas.has(nome)) {
          nome = `${nomeBase}${separador}${++contador}`;
        }
    
        return nome;
      }
    
      private aplicarEstiloEFormula(jsonPagina: any): void {
        this.atualizarEstilosNaPagina(jsonPagina);
        this.limparCamposTabela(jsonPagina);
      }
    
      private atualizarEstilosNaPagina(jsonPagina: any): void {
        if (this.opcoes?.temEstilo && jsonPagina?.data && this.mapaEstiloDoArquivoImportado?.size) {
          const atualizarEstilo = (objeto: any) => {
            if (objeto) {
              if (typeof objeto.style === 'string') {
                objeto.style = this.mapaEstiloDoArquivoImportado.get(objeto.style);
              } else if (objeto.style?.parentName) {
                objeto.style.parentName = this.mapaEstiloDoArquivoImportado.get(objeto.style.parentName);
              }
            }
          };
    
          [jsonPagina.data.columnDataArray, jsonPagina.data.rowDataArray].forEach((array) => {
            array?.forEach(atualizarEstilo);
          });
    
          if (jsonPagina.data.dataTable) {
            Object.values(jsonPagina.data.dataTable).forEach((row) => Object.values(row).forEach(atualizarEstilo));
          }
        }
      }
    
      private limparCamposTabela(jsonPagina: any): void {
        if (jsonPagina?.data?.dataTable) {
          Object.values(jsonPagina.data.dataTable).forEach((row) =>
            Object.values(row).forEach((celula) => {
              if (!this.opcoes.temEstilo) {
                celula.style = null;
              }
              if (!this.opcoes.temFormula) {
                celula.formula = null;
              }
            })
          );
        }
      }
  • Posted 6 December 2024, 3:29 am EST

    Hi,

    Currently, SJS does not support merging workbooks directly. The solution provided by the development team represents the best available workaround for merging workbooks at the moment. Unfortunately, there isn’t a more optimal approach to achieve this currently.

    Please note that merging large files into a workbook may cause issues due to resource limitations of the browser and native API.

    Regards,

    Priyam

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels