function generarDocumento() {
//Configuración
var colDirigidoA = 1;
var colExp = 2;
var colSector = 3;
var colCentro = 4;
var colObj = 5;
var colFecha = 6;
var colPartida = 7;
var colMonto = 8;
var colTipo = 9;
var colAnexo = 10;
var colPlanEst = 11;
var colObserv = 12;
//Identificaciones
const plantillaID = "1vqR6XPbafZ7_QNlmXAGaRWMi0lFkdd4Bk7H0eGtA1Aw";
const pdfID = "1Ey3HlODyVBHDjtAuzt5K4QW7da57Nhqj";
const tempID = "10z2ajdmcPDm00aFIvqlTly9hc57msLZ3";
//Conexiones
var doc = DocumentApp.openById(plantillaID);
var archivoPlantilla = DriveApp.getFileById(plantillaID);
const carpetaPDF = DriveApp.getFolderById(pdfID);
var carpetaTemp = DriveApp.getFolderById(tempID);
var hoja = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(2023);
//Variables para el doc
var filaActiva = SpreadsheetApp.getActiveRange().getRow();
var dirigidoA = hoja.getRange(filaActiva,colDirigidoA).getValue();
var sectorSolicitante = hoja.getRange(filaActiva,colSector).getValue();
var cDI = hoja.getRange(filaActiva,colCentro).getValue();
var expediente = hoja.getRange(filaActiva,colExp).getValue();
var objetoContratacion = hoja.getRange(filaActiva,colObj).getValue();
var partida = hoja.getRange(filaActiva,colPartida).getValue();
var monto = hoja.getRange(filaActiva,colMonto).getDisplayValue();
var anexoI = hoja.getRange(filaActiva,colAnexo).getValue();
//Crear copia
var copiaArchivoPlantilla = archivoPlantilla.makeCopy(carpetaTemp);
var copiaId = copiaArchivoPlantilla.getId();
var nombreDoc = "Afectación de "+expediente+" - "+objetoContratacion;
copiaArchivoPlantilla.setName(nombreDoc);
var doc = DocumentApp.openById(copiaId);
doc.setName(nombreDoc);
//Reemplazar variable
doc.getBody().replaceText("{{dirigidoA}}",dirigidoA);
doc.getBody().replaceText("{{sector}}",sectorSolicitante);
doc.getBody().replaceText("{{centro}}",cDI);
doc.getBody().replaceText("{{expediente}}",expediente);
doc.getBody().replaceText("{{objeto}}",objetoContratacion);
doc.getBody().replaceText("{{partida}}",partida);
doc.getBody().replaceText("{{monto}}",monto);
doc.getBody().replaceText("{{anexo}}",anexoI);
}
function onOpen(){
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu("Crear Afectaciones");
menu.addItem("Generar Documento Word","generarDocumento").addToUi();
}