Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,905 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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);
}
}