Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
Actualizar o crear una fila basada en ID - Google Script

Han pasado varios días desde que estoy atascado en este problema. Probé todo lo que vi, pero creo que echo de menos algo de lógica de Google Script.

Tengo un formulario php que envía datos por correo a una hoja de cálculo de Google. Utiliza un script de Google para manejar la solicitud y actualizar una hoja de cálculo de Google. El problema es que agregué una ID por fila (generada en mi aplicación web y enviada a través de mi formulario php) pero no puedo encontrar la manera de comparar la ID recibida en mi Google Script para compararla con la ID del última fila de mi hoja de cálculo. El objetivo es reemplazar la fila si la identificación ya existe o agregar la nueva fila al final de mi hoja de cálculo.

Aquí está el script de Google que encontré en Internet que funciona pero no maneja las actualizaciones de filas según la ID (las líneas comentadas son el código que intenté agregar, pero debido al hecho de que no puedo registrar datos cuando Estoy enviando mi formulario php, no puedo visualizar lo que me falta).

 var sheetName = 'Sheet1' var scriptProp = PropertiesService.getScriptProperties() function intialSetup () { var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet() scriptProp.setProperty('key', activeSpreadsheet.getId()) } function doPost (e) { var lock = LockService.getScriptLock() lock.tryLock(10000) try { var doc = SpreadsheetApp.openById(scriptProp.getProperty('key')) var sheet = doc.getSheetByName(sheetName) var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0] var nextRow = sheet.getLastRow() + 1 // var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues(); // var lastRow = sheet.getLastRow(); // var lastRowId = values[lastRow - 1][0]; // var test = JSON.parse(e.postData.contents); // Logger.log(test); // if (test.id == lastRowId) { // return; // } var newRow = headers.map(function(header) { return header === 'timestamp' ? new Date() : e.parameter[header] }) sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow]) return ContentService .createTextOutput(JSON.stringify({ 'result': 'success', 'row': nextRow})) .setMimeType(ContentService.MimeType.JSON) } catch (e) { return ContentService .createTextOutput(JSON.stringify({ 'result': 'error', 'error': e })) .setMimeType(ContentService.MimeType.JSON) } finally { lock.releaseLock() } }```
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Tal vez algo como esto

 function lfunko(obj) { const [hA, ...arr] = "obj.2d"; const idx = {}; hA.forEach((h, i) => { idx[h] = i; }); let idA = arr.map(r => r[idx["ID"]]); const ss = SpreadsheetApp.getActive(); const sh = ss.getSheetByName("SheetName"); const [h, ...vs] = sh.getDataRange().getValues(); let idx1 = {}; h.forEach((h, i) => { idx1[h] = 1; }) vo = [] vs.forEach((r, i) => { let n = idA.indexOf(idx1["ID"]) if (!~n) { vo.push(r) } else { vo.push(arr[n]) } }) Logger.log(JSON.stringify(vo)); sh.getRange(1,1,vo.length,vo[0].length).setValues(vo); }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!