json - 谷歌表格 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.');
}
}
解决方案
推荐阅读
- python - Python异步响应时间
- r - 是否有一种快速计算多列乘法结果的方法,但根据 NA 值具有不同的比例?
- reactjs - 反应,写入单选选项
- flutter - 在 TextField 中打印“newTaskTitle”的值时,它工作正常。但是当我尝试从 FlatButton 打印它时,它显示'null'
- python - 熊猫真正的外加入?
- python - scipy.lfilter 的替代品
- doctrine-orm - 准则 2.7.4:获取具有特定类型的实体
- stripe-payments - 使用 API ID 通过 Stripe 一次性付款
- typescript - 打字稿抱怨我没有使用未定义作为返回类型
- javascript - 如何根据项目在另一个数组中过滤数组?