text Google电子表格转换为JSON

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text Google电子表格转换为JSON相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.6.0/styles/monokai-sublime.min.css">
    <link rel="stylesheet" href="https://clipboardjs.com/bower_components/primer-css/css/primer.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.6.0/highlight.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.12/clipboard.min.js"></script>
    <script>
    hljs.initHighlightingOnLoad();
    </script>
  </head>
  <body>
    <h3> Generated JSON</h3>
    <div style="padding: 10px;margin-bottom:25px">
    <button id="clipboardbtn" class="btn" data-clipboard-target="#foo">
     <img class="clippy" src="https://clipboardjs.com/assets/images/clippy.svg" width="13" alt="Copy to clipboard">
    </button><span style="margin-left: 5px">Click on the button to copy</span>
    </div>
    <pre><code class="json" id="foo"><?= data ?></code></pre>
    <script>
    //Tooltip functions
    var btn = document.getElementById("clipboardbtn");btn.addEventListener('mouseleave',function(e){e.currentTarget.setAttribute('class','btn');e.currentTarget.removeAttribute('aria-label');});
    function showTooltip(elem,msg){elem.setAttribute('class','btn tooltipped tooltipped-s');elem.setAttribute('aria-label',msg);}
    function fallbackMessage(action){var actionMsg='';var actionKey=(action==='cut'?'X':'C');if(/iPhone|iPad/i.test(navigator.userAgent)){actionMsg='No support :(';}
      else if(/Mac/i.test(navigator.userAgent)){actionMsg='Press ⌘-'+ actionKey+' to '+ action;}
      else{actionMsg='Press Ctrl-'+ actionKey+' to '+ action;}
      return actionMsg;
    } 
    //Clipboard definition and success/error handlers
    var clipboard = new Clipboard('#clipboardbtn');
    clipboard.on('success',function(e){
      //e.clearSelection();
      showTooltip(e.trigger,'Copied!');});
    clipboard.on('error',function(e){
     showTooltip(e.trigger,fallbackMessage(e.action));});
    </script>
  </body>
</html>

function doGet(e) {
  var book = SpreadsheetApp.getActiveSpreadsheet();
  var masterSheet = book.getSheetByName("New_072018");
//  var json = convertSheet2Json(masterSheet);
  var colStartIndex = 1;
  var rowNum = 1;
  var firstRange = masterSheet.getRange(1, 1, 1, masterSheet.getLastColumn());
  var firstRowValues = firstRange.getValues();
  var titleColumns = firstRowValues[0];

  // after the second line(data)
  var lastRow = masterSheet.getLastRow();
  var ui = SpreadsheetApp.getUi();

  var rowValues = [];
  for(var rowIndex=2; rowIndex<=lastRow; rowIndex++) {
    var colStartIndex = 1;
    var rowNum = 1;
    var range = masterSheet.getRange(rowIndex, colStartIndex, rowNum, masterSheet.getLastColumn());
    var values = range.getValues();
    rowValues.push(values[0]);
  }
  

  
  // create json
  var json = [];
  for(var i=0; i<rowValues.length; i++) {
    var line = rowValues[i];
    var jsontemp = new Object();
    for(var j=0; j<titleColumns.length; j++) {
      jsontemp[titleColumns[j]] = line[j];
    }
    json.push(jsontemp);
   }

  var t = HtmlService.createTemplateFromFile('index');
  t.data = JSON.stringify(json);    
  var html = t.evaluate();
  html.setTitle("Generated JSON here");
  html.setWidth(500);
  SpreadsheetApp.getUi().showSidebar(html);
}

function onOpen() {
  SpreadsheetApp.getUi()
  .createMenu("RC Stuff")
  .addSeparator()
  .addItem("Generate JSON", "doGet")
  .addToUi();
}

以上是关于text Google电子表格转换为JSON的主要内容,如果未能解决你的问题,请参考以下文章

将excel电子表格转换为json

如何将 Google 电子表格的工作表字符串 id 转换为整数索引 (GID)?

将带有表格数据的 Excel 电子表格转换为 JSON 的最简单方法是啥? [关闭]

如何将 Google 电子表格的工作表字符串 id 转换为整数索引 (GID)?

如何将Google Visualization API的日期列转换为JSON格式

将关联数组转换为数字以在Apps脚本中写入工作表