Lo primero es el código para el menú para llamar el script desde ahí.
la función processForm es muy importante para poder hacer la conversión ya que es el vinculo que enlace el excel para ser convertido
Paso a Paso:
se debe crear la función onOpen para llamar el código desde ahí
crear el index.html (ver código al fina)
crear la función doGet
crear la función processForm
ejecutar el doGet para crear la aplicación web y tomar nota de la url creada
crear la función importDLocal
ejecutar desde el menú
Consulta el código y el archivo HTML al final de este articulo 🔎
si el tema fue de utilidad comparte este articulo con otras personas, nos ayudarías mucho haciendo difusión de estas propuestas 😉
function onOpen(e){
SpreadsheetApp.getUi().createMenu('🚀 Excel a GSheets')
.addItem("📌 1.-Convertir desde Drive","convertirExcel_a_GoogleSheets_2")
.addItem("🔔 2.-Importar de Disco Local y convertir","importDLocal")
.addItem("💎 3.-Convertir con Drive-Picker","showPicker")
.addToUi();
}
//============= Ejemplo #2.- Importar desde Archivo local y convertitr ================================
//
//=====================================================================================================
/* esta funcion o script muestra la web app en una ventanflotante en la interfaz de GSheet
para ejecutarla desde ese entorno. */
function importDLocal(){
var contenedor = '<style>iframe{width:100%;height:320px;border:none;}</style>'+
'<iframe src="https://script.google.com/macros/s/AKfycbwRKs0Mn5yrwZmIM0BtFDVmzD9mf3EehV3zxXvBTdk/exec"></iframe>';
var html = HtmlService.createHtmlOutput(contenedor).setHeight(340).setWidth(550);
SpreadsheetApp.getUi().showModelessDialog(html, '🔎 Buscar archivo de Excel a convertir') //.showSidebar(html);
}
<!DOCTYPE html>
<html>
<head>
<style> /* sea gregaron todos los estilos que no estan en el original */
.cont{padding:30px;background-color:lightgreen;border-radius:10px;width:70%;margin:auto;}
#cargar{width:120px;border-radius:6px;border:none;}
h2{color:green}
input{padding:8px;background-color:black;color:white;border-radius:4px;width:100%;}
input:hover{background-color:blue}
</style>
<base target="_top">
<script>
// Prevent forms from submitting.
function preventFormSubmit() {
var forms = document.querySelectorAll('form');
for (var i = 0; i < forms.length; i++) {
forms[i].addEventListener('submit', function(event) {
event.preventDefault();
});
}
}
window.addEventListener('load', preventFormSubmit);
function handleFormSubmit(formObject) {
google.script.run.withSuccessHandler(updateUrl).processForm(formObject);
}
function updateUrl(url) {
var div = document.getElementById('output');
div.innerHTML = '<b><a href="' + url + '" target="_blank">ver archivo!</a></b>';
}
</script>
</head>
<body>
<div class="cont">
<h2>Enviar y convertir excel a mi Drive</h2>
<form id="myForm" onsubmit="handleFormSubmit(this)">
<input name="myFile" type="file" />
<br><br>
<input id="cargar" type="submit" value="Enviar" />
</form>
<br>
<div id="output"></div>
<p><i>Nota: el proceso puede tardar un poco</i></p> <!-- se agrego esta linea como nota -->
</div>
</body>
</html>
/* ejecutar doGet y despues ir al index y crear una nueva implementación y el exce copiarlo en la funcion importDLocal
o sustituir por la mas reciente implementacion ambiando la terminacion "dev" por "exec" */
function doGet(){
return HtmlService.createHtmlOutputFromFile('index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)//Cambio #1
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);//Cambio #2
}
function processForm(formObject) {
var formBlob = formObject.myFile;
//------------ inicia adaptacion ----------------------------------------------
//ver resultado en folder: https://drive.google.com/drive/folders/1B6UBUnt1rc_QoQPXOgE171bsc-gOnhvF
var carpetPrueba = DriveApp.getFolderById("1B6UBUnt1rc_QoQPXOgE171bsc-gOnhvF");//Cambio #3
var driveFile = carpetPrueba.createFile(formBlob);//Cambio #4
var archivo = deExcelaGShets(driveFile.getName());//convertimos con la fun aux y obtenemos el objeto(#5)
var nombreGSheet = archivo.nombre;//obtenemos el nombre del archivo (ver funcion auxiliar)(#6)
var url = archivo.url;//obtenemos la url del archivo (#7)
var id = archivo.id;//obtenemos su id todo desde el objeto "archivo" que nos da la fun auxiliar (#8)
var ss = SpreadsheetApp.openById("1imDOhKTJkaw9BZ5b8Ui7MWsUht_rFr6Q86r0fYNVnWs");//Cambio #9.-nvo archvo
var hoja = ss.getSheetByName("Hoja 2");//Cambio #10
var marcaTemp = Utilities.formatDate(new Date(), "GMT", "dd-MM-yyyy HH:mm:ss");//Cambio #11
hoja.appendRow([marcaTemp, nombreGSheet,url,id]);//Cambio #12
//return driveFile.getUrl();
return url; //regresa la url que se pasará como parametro a la web app para mostrar el enlace.(#13)
//------------ termina adaptación ---------------------------------------------
}
Síguenos y suscríbete en: