// Script para Google Sheets con visor 3D interactivo y gestión de productos/pedidos // Hojas: "Productos" y "Pedidos" // Variables globales var productosSheet = "Productos"; var pedidosSheet = "Pedidos"; var productosData = []; var stlFiles = {}; var currentColor = "#000000"; var currentModel = null; // Inicializar al abrir el documento function onOpen() { var ui = SpreadsheetApp.getUi(); ui.createMenu('3D Viewer') .addItem('Agregar al carrito', 'addToCart') .addSeparator() .addItem('Limpiar carrito', 'clearCart') .addToUi(); // Cargar datos de productos loadProducts(); // Inicializar visor 3D init3DViewer(); } // Cargar productos desde la hoja de cálculo function loadProducts() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(productosSheet); if (!sheet) return; var data = sheet.getDataRange().getValues(); productosData = []; for (var i = 1; i < data.length; i++) { if (data[i][0] && data[i][1]) { productosData.push({ nombre: data[i][0], precio: data[i][1], vida: data[i][2], inventario: data[i][3], id: data[i][4] }); } } Logger.log('Productos cargados: ' + productosData.length); } // Inicializar visor 3D function init3DViewer() { // Crear HTML para el visor var html = HtmlService.createHtmlOutputFromFile('viewer') .setWidth(400) .setHeight(400); SpreadsheetApp.getUi().showModelessDialog(html, 'Visor 3D'); } // Función para agregar al carrito function addToCart(productName, quantity, client) { // Buscar el producto var product = productosData.find(p => p.nombre.toLowerCase() === productName.toLowerCase() ); if (!product) { SpreadsheetApp.getUi().alert('Producto no encontrado: ' + productName); return; } if (product.inventario <= 0) { SpreadsheetApp.getUi().alert('Producto sin inventario: ' + productName); return; } // Agregar a la hoja de pedidos var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(pedidosSheet); if (!sheet) { SpreadsheetApp.getUi().alert('Hoja de pedidos no encontrada'); return; } sheet.appendRow([ product.nombre, quantity, client, product.inventario, product.id ]); // Actualizar inventario updateInventory(product.nombre, -quantity); SpreadsheetApp.getUi().alert('Producto agregado al carrito: ' + product.nombre); } // Actualizar inventario function updateInventory(productName, quantity) { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(productosSheet); if (!sheet) return; var data = sheet.getDataRange().getValues(); for (var i = 1; i < data.length; i++) { if (data[i][0] && data[i][0].toLowerCase() === productName.toLowerCase()) { var currentInv = parseInt(data[i][3]) || 0; var newInv = currentInv + quantity; sheet.getRange(i+1, 4).setValue(newInv); break; } } } // Limpiar carrito (eliminar últimas filas de pedidos) function clearCart() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(pedidosSheet); if (!sheet) return; var lastRow = sheet.getLastRow(); if (lastRow > 1) { sheet.deleteRows(2, lastRow - 1); SpreadsheetApp.getUi().alert('Carrito limpiado'); } } // Cargar STL files (simulado - en producción conectaría con Google Drive o base de datos) function loadSTLFiles() { // Esta función simula la carga de archivos STL // En producción, conectaría con Google Drive API o base de datos stlFiles = { 'greca': 'data/stl/greca.stl', 'minimalista': 'data/stl/minimalista.stl', 'clásico': 'data/stl/clásico.stl', 'moderno': 'data/stl/moderno.stl' }; Logger.log('STL files cargados: ' + Object.keys(stlFiles).length); } // Cambiar color del modelo 3D function changeColor(color) { currentColor = color; // Enviar mensaje al visor para actualizar color var app = UiApp.getActiveApplication(); if (app) { app.getElementById('modelColor').setStyleAttribute('color', color); } SpreadsheetApp.getUi().showModelessDialog( HtmlService.createHtmlOutput( '