├── README.md └── app_script.gs /README.md: -------------------------------------------------------------------------------- 1 | # gas-rest-api 2 | # gas-rest-api 3 | -------------------------------------------------------------------------------- /app_script.gs: -------------------------------------------------------------------------------- 1 | var ss = SpreadsheetApp.openById("1XPLvup4IUy4Ffeim8211rh1vRdDiTE2c6e9UtRSPGHk"); 2 | var sheet = ss.getSheetByName("app data") 3 | var rng = ss.getDataRange() 4 | var data = rng.getValues() 5 | var headings = data[0] 6 | 7 | /* Take a product ID as input and return the 8 | * row corresponding to that product ID.*/ 9 | 10 | function productQuery(prodId){ 11 | for (var i = 1; i < data.length; i++){ 12 | if (prodId === data[i][0]){ 13 | return data[i] 14 | } 15 | } 16 | } 17 | 18 | /* Take a spreadsheet (product) row and turn it into an object 19 | with spreadsheet headings as object keys. */ 20 | 21 | function formatProduct(rowData){ 22 | var product = {} 23 | for (var i = 0; i < headings.length; i++){ 24 | product[headings[i].toString()] = rowData[i] 25 | } 26 | return product 27 | } 28 | 29 | function doGet(request) { 30 | // Check for a valid request URI 31 | if (request.parameter.action !== undefined){ 32 | if (request.parameter.prodid !== undefined){ 33 | prodIds = request.parameters.prodid 34 | 35 | // The object to be returned as JSON 36 | response = { 37 | products : [] 38 | } 39 | 40 | // Fill the products array with requested products 41 | for (var i = 0; i < prodIds.length; i++){ 42 | sheetData = productQuery(prodIds[i]) 43 | product = formatProduct(sheetData) 44 | response.products.push(product) 45 | } 46 | 47 | if (response.products.length > 0){ 48 | return ContentService.createTextOutput(JSON.stringify(response)); 49 | } else { 50 | return ContentService.createTextOutput('Invalid Request. Product ID(s) not found.'); 51 | } 52 | } else { 53 | return ContentService.createTextOutput('Invalid Request. Use at least one valid "prodid" parameter.'); 54 | } 55 | } else { 56 | return ContentService.createTextOutput('Invalid Request. Use a valid "action" parameter.'); 57 | } 58 | } 59 | --------------------------------------------------------------------------------