Why Am I Getting the Error Message 'WORKBOOK REPAIRED' Every Time I Reopen My Excel Workbook After Running Office Scripts Code?

Husiel Ruiz 0 Reputation points
2024-03-21T18:40:16.8933333+00:00

WORKBOOK REPAIRED

We temporarily repaired this workbook so that you can open it in Reading View.

Every time I execute this Office Scripts code, close and reopen the workbook, I get this error. I've reviewed the code but it seems fine to me. What could it be? I'm sharing my code with you: interface EmpresaData {

  id: number;
  ruc: string;
  razonSocial: string;
  nombreComercial: string;
}
class Empresa {
  constructor(public id: number, public ruc: string, public razonSocial: string, public nombreComercial: string) { }
}
function obtenerDatosCelda(hoja: ExcelScript.Worksheet, celda: string): string {
  return String(hoja.getRange(celda).getValue());
}
function limpiarContenidoCelda(hoja: ExcelScript.Worksheet, celda: string): void {
  hoja.getRange(celda).clear(ExcelScript.ClearApplyTo.contents);
}
function main(workbook: ExcelScript.Workbook) {
  const nombreHojaTabla: string = "Empresas";
  const nombreTabla: string = "Empresas";
  const hojaEmpresas = workbook.getWorksheet(nombreHojaTabla);
  const tabla = hojaEmpresas.getTables().find(t => t.getName() === nombreTabla);
  const ruc = obtenerDatosCelda(hojaEmpresas, "C2");
  const razonSocial = obtenerDatosCelda(hojaEmpresas, "C3");
  const nombreComercial = obtenerDatosCelda(hojaEmpresas, "C4");
  const idList: number[] = tabla.getColumnById(1).getRange().getValues().slice(1).map(fila => Number(fila[0]));
  const nuevoId = Math.max(...idList, Number.MIN_VALUE) + 1;
  const nuevaEmpresaData: EmpresaData = {
    id: nuevoId,
    ruc: ruc,
    razonSocial: razonSocial,
    nombreComercial: nombreComercial
  };
  if (ruc.length > 0 && razonSocial.length > 0) {
    tabla.addRow(-1, [nuevaEmpresaData.id, nuevaEmpresaData.ruc, nuevaEmpresaData.razonSocial, nuevaEmpresaData.nombreComercial]);
  }
  limpiarContenidoCelda(hojaEmpresas, "C2");
  limpiarContenidoCelda(hojaEmpresas, "C3");
  limpiarContenidoCelda(hojaEmpresas, "C4");
  returnLista();
  function returnLista(): void {
    const hojaPrincipal = workbook.getWorksheet("Principal");
    const hojaDetalleServicio = workbook.getWorksheet("Detalle_Servicio");
    const hojaPagos = workbook.getWorksheet("Pagos");
    const datosTablaEmpresa = tabla.getRangeBetweenHeaderAndTotal().getValues();
    const valoresConcatenados: string[] = datosTablaEmpresa.map(fila => fila[3] + ' - ' + fila[1]);
    const cadenaSeparadaPorComas = valoresConcatenados.join(',');
    const targetCell = hojaPrincipal.getRange("C2");
    const dataValidation = targetCell.getDataValidation();
    const targetCell2 = hojaDetalleServicio.getRange("C2");
    const dataValidation2 = targetCell2.getDataValidation();
    const targetCell3 = hojaPagos.getRange("C2");
    const dataValidation3 = targetCell3.getDataValidation();
    const validationCriteria: ExcelScript.ListDataValidation = {
      inCellDropDown: true,
      source: cadenaSeparadaPorComas
    };
    const validationRule: ExcelScript.DataValidationRule = {
      list: validationCriteria
    };
    dataValidation.setRule(validationRule);
    dataValidation2.setRule(validationRule);
    dataValidation3.setRule(validationRule);
  }
}
Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,905 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
983 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,918 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.