[精讚] [會員登入]
3231

將google試算表當作簡易資料庫,利用Google apps cript 在網頁上操作查詢

將google試算表當作簡易資料庫,利用apps cript 在網頁上操作查詢 若我有一試算表資料 縣市 status

分享此文連結 //n.sfs.tw/14621

分享連結 將google試算表當作簡易資料庫,利用Google apps cript 在網頁上操作查詢@igogo
(文章歡迎轉載,務必尊重版權註明連結來源)
2020-05-17 00:01:34 最後編修
2020-04-29 11:51:39 By igogo
 

 

 

將Google試算表當作簡易資料庫,利用Google apps cript 在網頁上操作查詢

 

若我有一試算表資料

縣市	status	aqi
新北市	普通	62
臺北市	普通	66
桃園市	普通	61
臺中市	良好	44
臺南市	普通	56
高雄市	普通	77
宜蘭縣	普通	74

 

要如何在web上操作查詢, 要分成前後端來看

後端是google apps script

function doGet(e) {

  let params = e.parameters;  
  let url = ScriptApp.getService().getUrl();
//  return HtmlService.createHtmlOutput(url);
  
  return HtmlService.createTemplateFromFile('index')
  .evaluate();
 
}



function getMsg() {
  return 'Hello,world';
}

function getItems(name){
  Logger.log("query name:"+name);
  let url = 'https://docs.google.com/spreadsheets/d/14bY56g1SuMleCfY-67pBkfJi0j7exj04aLvOYopSEj0/edit?usp=sharing';
  let SpreadSheet = SpreadsheetApp.openByUrl(url);
  let sheet = SpreadSheet.getSheets()[0];
  let lastRow = sheet.getLastRow();
  let items = [];
  
  //init get all items
  
  if(name === "init"){
    for(let i=2; i<lastRow+1; i++){    
      let item = {};
     
      item.name = sheet.getSheetValues(i, 1,1,1)[0][0];
      item.status = sheet.getSheetValues(i, 2,1,1)[0][0];
      item.aqi = sheet.getSheetValues(i, 3,1,1)[0][0];
      items.push(item);   
    }
  } else {
    for(let i=2; i<lastRow+1; i++){    
      let item = {};
      item.name = sheet.getSheetValues(i, 1,1,1)[0][0];
      item.status = sheet.getSheetValues(i, 2,1,1)[0][0];
      item.aqi = sheet.getSheetValues(i, 3,1,1)[0][0];  
      if(name === item.name){
        items.push(item);  
      }
       
    }
  } 
  

//  Logger.log(items);
  return items;
}

 

doGet() 是處理 http get的方式

getItems()則是讀取試算表的資料

 

前端面頁 index.html

<!DOCTYPE html>
<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <base target="_top">
  </head>
  <body>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> </script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  
<div class="container">
  <div class="row h-100 justify-content-center align-items-center">
    <br>
    <div id="app">
       <br>
       <br>
       <br>
       <form>
  <div class="row">
    <div class="col">
       <select v-model="selected">
              <option v-for="choice in choices" v-bind:value="choice">
              {{ choice }}
              </option>
          </select>
    </div>
    <div class="col">
         <button type="button" @click="getItem" class="btn btn-primary">查詢</button>    
     
    </div>
  </div>
</form>
                
         <!-- {{items}} -->
        <table class="table">
        <thead>
        <tr>
        
        <th scope="col">縣市</th>
        <th scope="col">status</th>
        <th scope="col">aqi</th>
        </tr>
        </thead> 
          <tbody>
            <tr v-for="item in items">
            
              <td>{{item.name}}</td>
              <td>{{item.status}}</td>
              <td>{{item.aqi}}</td>
           </tr>
          </tbody>
        </table>    
                  
    </div>
  </div>      
</div>   

<script>
  //created stage
  google.script.run.withSuccessHandler(onCreated).getMsg();  
  google.script.run.withSuccessHandler(onCreatedItems).getItems("init");
  
 
 var app = new Vue({
  el: '#app',
  data: {
    msg: '',
    items:[],
    choices:[],
    selected:'',
  },   
  methods: {
    getItem: function () {
      console.log(this.selected);
      google.script.run.withSuccessHandler(onCreatedItems).getItems(this.selected);
    }
  }
 })
   
 
function onCreated(msg) {
  console.log(msg);
  app.msg = msg; 
}


function onCreatedItems(items) {
  console.log("query app selected:" + app.selected);
  console.log("get items: "+items);
  app.items = items; 
  if(app.selected===''){
  items.forEach(item=>{   
    //選項不重覆
    if(!app.choices.includes(item.name)){       
      app.choices.push(item.name);
    }
  })
     
    
  }
  
}
 
</script>


</body>
</html>


 

 

 

為了要方便操作DOM,  我使用了vue.js,  最主要的語法在callback 這一段

google.script.run.withSuccessHandler(onCreatedItems).getItems("init");

參考 https://developers.google.com/apps-script/guides/html/reference/run

畫面

 

查詢縣市

 

 

END

你可能感興趣的文章

[vue.js] 動態的props 做parent-child components 雙向綁定 vue.js props components camel-case

vue.js modal 作兩個選項按鈕並導向不同頁面 vue.js modal 作兩個選項按鈕

javascript 陣列 javascript 陣列可以放各种型別的元素 let data = [1,2,"john",tru

word題目轉google測驗 word題目轉google測驗

將google試算表當作簡易資料庫,利用Google apps cript 在網頁上操作查詢 將google試算表當作簡易資料庫,利用apps cript 在網頁上操作查詢 若我有一試算表資料 縣市 status

axios vuejs application/x-www-form-urlencoded 送資料 VUE.JS 以 application/x-www-form-urlencoded 送資料

隨機好文

[vue.js] 設定 content type 今天在wickt 端怎麼就是收不到vue.js 以post 傳過來的資料 找了好久才發現 application/jso

2018 hoc 頒獎 校慶到了,啦啦隊比賽如火如荼展開,學務主任將頒發獎狀給表現優異的班級。請完成以下程式碼,讓程式將啦啦隊表演成績由高至低依序輸出。

2018 hoc 掃地機器人 掃地機器人只能打掃沒有障礙物(桌椅、牆壁)的範圍,請寫程式控制機器人打掃餐廳的所有走道, 並在清掃完畢後回到充電器。

hoc2018灑水機器人 灑水機器人的工作是替行道樹灑水,機器人的灑水範圍有限(左前方、左方、左後方),請寫程式控制機器 人判斷須灑水的狀況。每顆

[scratch2] 分數排名 在清單中隨机產生5名學生的考試分數, 再利用另一個清單排名 想法, 分數愈高者排名愈好, 例如名次是第5名, 那分數是最