En mi Hoja de Google tengo una lista de libros con sus detalles. El usuario inserta el título que está buscando, jQuery pasa el título a una función en GAS y luego devuelve las coincidencias (libros encontrados) al JS.
Aquí mi código.
HTML
<form id="formD"> <input type="text" id="title" name="title" placeholder="Math"> <button id="submit">Search</button> </form>JS
$("#submit").click(function() { var data = []; data.push($("#title").val()); //title the user is looking for google.script.run.withSuccessHandler(onSuccess).find(data); }); function onSuccess(a) { alert(a); //example }GAS
function doGet() { return HtmlService.createTemplateFromFile('index') .evaluate() .addMetaTag('viewport', 'width=device-width, initial-scale=1'); } function find(data) { var sheet = SpreadsheetApp.openById("XXX").getSheetByName("XXX"); var range = sheet.getRange("A:G"); var values = range.getValues(); var result = []; var title = data[0]; for(i=0; i < values.length; i++){ if(values[i][0] != "") { if(values[i][2].includes(title)) result.push(values[i]); } } return result }No sé cómo analizar y usar la matriz que paso a GAS. Cuando se hace clic en el botón "buscar", la página se vuelve a cargar y el valor se agrega a la URL.
function find(data) { var sheet = SpreadsheetApp.openById("XXX").getSheetByName("XXX"); var range = sheet.getRange("A1:G" + sheet.getLastRow()); var values = range.getValues(); var result = []; var title = data; for (let i = 0; i < values.length; i++) { if (values[i][0] != "") { if (values[i][2].includes(title)) result.push(values[i]); } } return result; } function onSuccess(a) { alert(a.join('\n')); //example }js:
$("#submit").click(function() { var data = $("#title").val(); google.script.run.withSuccessHandler(onSuccess).find(data); });Aquí está la solución al problema de que al hacer clic en el botón se recarga la página.
$("#sumbit").click(function(){ }Este fragmento de código estaba fuera de $(document).ready(function(){}).