首页 > 解决方案 > 谷歌表格 API 参数

问题描述

我有一个 appScript API 代码,它从谷歌表格返回 JSON 数据我想根据发送的请求参数搜索数据,例如,如果发送的参数大于 90000,则使用 Mobile 字段搜索数据,如果小于 90000,则使用 PatCode 搜索数据场地。

附加代码以及数据头的快照

var ss = SpreadsheetApp.openById("XXXXXXXXXXXXXXXXXXXX");
var sheet = ss.getSheetByName("Data")
var rng = ss.getDataRange()
var data = rng.getValues()
var headings = data[0]

/* Take a product ID as input and return the
 *  row corresponding to that product ID.*/

function patientQuery(q)
{

  
 let dataList = [];
 for (var i = 1; i < data.length; i++)
   {
     if (q === data[i][3])
     {
       dataList.push(data[i]); // This should return data[i] instead of the first row which is the header
     }
   }

  return dataList; // Return a list of products instead
}

/* Take a spreadsheet (product) row and turn it into an object
 with spreadsheet headings as object keys.*/


function formatProduct(rowData){
 var product = {}
 for (var i = 0; i < headings.length; i++){
   product[headings[i].toString()] = rowData[i]
 }
 return product
}


function doGet(request) {
 // Check for a valid request URI

   if (request.parameter.q !== undefined){
     let Mobile = request.parameters.q
   
     // The object to be returned as JSON
     let response = {
       patients : []
     }
    
     // Fill the products array with requested products
     for (var i = 0; i < Mobile.length; i++){  
     /** CHANGES - START*/
       patientQuery(Mobile[i]).forEach((sheetData, index) => { // Iterate trough the items list
         let product = formatProduct(sheetData)
         product.Serial = index + 1
         response.patients.push(product)
       })
       
     /** CHANGES - END*/
     }
    
     if (response.patients.length > 0){
       return ContentService.createTextOutput(JSON.stringify(response));
     } else {
       //return ContentService.createTextOutput('Invalid Request. Product ID(s) not found.');
       return ContentService.createTextOutput('No recodrs found.');
     }     
   } else {
           return ContentService.createTextOutput('Invalid Request. Use at least one valid "Mobile number" 
parameter.');
   }

}

数据

标签: jsongoogle-apps-scriptgoogle-sheets-api

解决方案


推荐阅读