Hola Amigos del foro mediante este codigo que he venido trabajando
Tengo una archivo Origen donde tengo una hoja con esta estructura
CodigoFechaReferenciaDescripcionSaldoMediante un query lo que quiero es seleccionar el codigo de la cuenta y que se actualice la tabla luego esa tabla con los titulos empiezan en la a1 y temina en la e5 copiarla al archivo destino creando la hoja sino existe de existir deberia copiar el rango despues del titulo a6 hasta la ultimafila con datos.
El proceso de creacion de la hoja con el nombre y la copia de los titulo y el rango de datos funciona bien, el problema es que cuando la hoja existe no me esta informando que ya existe y deberia copiar del rango a6 hasta la ultima fila con dato del archivo origen y copiarlo en la ultimafila usada en la hoja destino...
Su ayuda que deberia corregir
Anexo el codigo que estoy usando
function crearAnalisisNuevoGS052024() {
try {
// Replace these with your actual IDs
const sourceSpreadsheetId = '1GjCb4DEktYT3f-XvjqhlHjMLF85Dw5G2BiABvdYWQ9E';
const destinationSpreadsheetId = '1zMAt_I-nc5s2_b1T_lr9jzTLawfLRz60G6YlnaEvFgI';
// Get the source and destination spreadsheets
const sourceSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetId);
const destinationSpreadsheet = SpreadsheetApp.openById(destinationSpreadsheetId);
// Get the source sheet and account name from cell D1
const sourceSheet = sourceSpreadsheet.getSheetByName("Mayor");
const accountName = sourceSheet.getRange('D1').getValue();
// Check if the sheet already exists in the destination spreadsheet
if (existeHoja(destinationSpreadsheet, accountName)) {
// If the sheet exists, ask the user if they want to overwrite the data
var respuesta = Browser.msgBox("La hoja " + accountName + " ya existe en el libro destino. ¿Desea copiar la data al final de la hoja?");
if (respuesta === "Sí") {
// If the user says "Yes", copy the data to the existing sheet
copiarDatosExistente(sourceSpreadsheet, sourceSheet, accountName, destinationSpreadsheet);
}
} else {
// If the sheet doesn't exist, create a new sheet and copy the data
copiarDatosNueva(sourceSpreadsheet, sourceSheet, accountName, destinationSpreadsheet);
}
} catch (error) {
Logger.log("Error al crear análisis: " + error.message);
// Handle the error appropriately (e.g., notify the user, send an email, etc.)
}
}
// Function to copy data to an existing sheet
function copiarDatosExistente(sourceSpreadsheet, sourceSheet, accountName, destinationSpreadsheet) {
const destinationSheet = destinationSpreadsheet.getSheetByName(accountName);
const lastRowSource = sourceSheet.getLastRow();
const lastRowDestination = destinationSheet.getLastRow() || 0;
const lastRow = Math.max(lastRowSource, lastRowDestination);
const dataRangeSource = sourceSheet.getRange(6, 1, lastRowSource, 6).getValues(); // Start from row 6 (excluding header)
let lastEmptyRowDestination = 0;
for (let row = lastRowDestination; row >= 1; row--) {
if (destinationSheet.getRange(row, 1).getValue() === "") {
lastEmptyRowDestination = row;
break;
}
}
const dataRangeDestination = destinationSheet.getRange(lastEmptyRowDestination + 1, 1, dataRangeSource.length, dataRangeSource[0].length);
dataRangeDestination.setValues(dataRangeSource);
}
// Function to copy data to a new sheet
function copiarDatosNueva(sourceSpreadsheet, sourceSheet, accountName, destinationSpreadsheet) {
const newSheet = destinationSpreadsheet.insertSheet();
newSheet.setName(accountName);
const destinationSheet = destinationSpreadsheet.getSheetByName(accountName);
const dataRangeSource = sourceSheet.getRange(1, 1, sourceSheet.getLastRow(), 6).getValues(); // Include header row
const dataRangeDestination = destinationSheet.getRange(1, 1, sourceSheet.getLastRow(), 6);
dataRangeDestination.setValues(dataRangeSource);
}
// Function to check if a sheet exists in a spreadsheet
function existeHoja(spreadsheet, sheetName) {
const sheets = spreadsheet.getSheets();
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].getName() === sheetName) {
return true;
}
}
return false;
}