[精讚] [會員登入]
2717

將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

你可能感興趣的文章

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

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

題庫批次匯入google表單 請先建一新試算表, 將題目轉成格式如下 並將網址列記下來, 後續的題目就是從此試算表讀出 題目 答案 選項一 選項二 選

vue.js component 在parent與child 傳值 component 在parent與child 傳值

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

vue.js components 多個組件的呈現 vue.js 組件 component

隨機好文

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

java.time 時間 instant java.time

臺中市雲端校務系統與Windows AD帳號整合(4) 雲端校務系統與Windows AD的架構

臺中市雲端校務系統與Windows AD帳號整合(7) 設定校端更改密碼程式及執行

臺中市雲端校務系統與Windows AD帳號整合(8) 三 開機啟動服務 3-1 Windows Server上隨系統開機啟動服務 寫一個bat檔 cd C:UsersAdmi