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

118
Views
API de AppScript que devuelve un objeto JSON vacío

Entonces, estoy tratando de construir una API usando mi Hoja de Google y me encontré con un problema.

Aquí está mi código

 function doGet() {
 let doc = SpreadsheetApp.getActiveSpreadsheet();
 let sheet = doc.getSheetByName('portfolios');
 let values = sheet.getDataRange().getValues();

 let output = [];
 for (let i = 2; i < values.length; i++) {
 let row = [];
 row['name'] = values[i][0];
 row['committee'] = values[i][1];
 row['post'] = values[i][2];
 //console.log(row)
 output.push(row);
 }

 console.log(output)

 return ContentService.createTextOutput(JSON.stringify(output)).setMimeType(ContentService.MimeType.JSON);
}

Cuando ejecuto el script, obtengo el resultado deseado en la consola, pero cuando implemento la API (configuré "Quién tiene acceso" para Todos) y envío una solicitud GET, recibo un objeto JSON vacío.

[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]

¿Qué estoy haciendo mal?

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

En nombre de OP, Himanshu Sardana

Pude corregir el error, resulta que se debió a que usé la función JSON.stringify incorrectamente. Pude arreglarlo cambiando:

 let row = [];

a

 let row = {};

Código editado:

 function doGet() {
 let doc = SpreadsheetApp.getActiveSpreadsheet();
 let sheet = doc.getSheetByName('portfolios');
 let values = sheet.getDataRange().getValues();

 let output = [];
 for (let i = 2; i < values.length; i++) {
 let row = {}; // Edit in question
 row['name'] = values[i][0];
 row['committee'] = values[i][1];
 row['post'] = values[i][2];
 //console.log(row)
 output.push(row);
 }

 console.log(output)

 return ContentService.createTextOutput(JSON.stringify(output)).setMimeType(ContentService.MimeType.JSON);
}

almost 4 years ago · Santiago Trujillo 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!