如何在Google电子表格对话框之间传递数据?

时间:2017-02-22 22:55:28

标签: javascript google-apps-script google-sheets

我正在尝试构建基于Google电子表格的订单系统。 我有1张电子表格(“订单”),有2张(“订单”和“产品”)。 首先是完全空白(它将是我的对话框将放置数据的工作表。 第二个是带有文章列表的简单数据库:

  • A栏[ID]
  • B栏[品牌]
  • C栏[PRODUCT]
  • D栏[PIECES]
  • E栏[PRICE]
  • 列F [CATEGORY]
  • G栏[UM]
  • H栏[增值税]
  • 第I栏[SUBCATEGORY]

一个品牌可以在一个或多个类别中拥有多个产品...... 我得到了

  • 1 Code.gs

和4个HTML对话框

  • Scelta Categoria.html
  • Scelta Marchio.html
  • Scelta Prodotto.html
  • Ordini.html

这里我没有放置Scelta Prodotto.html或Order.html代码,因为首先我需要在其他对话框之间传递数据。 如果可能,我不想使用网络应用,而是使用自定义对话框工作的简单电子表格。 我不是在寻找与我的客户共享的电子表格,而是为每个我的客户提供更多的电子表格(每个=另一个)(这是一个小生意,只有30个客户,并非所有人都可以使用互联网或电脑发送我的订单(他们中的大多数使用电话打电话给我,并命令我订购)。 对于那些比我更现代化的人,我开始开发那些(如谷歌在他的脚本指南中所说)脚本绑定到Google表格。

code.gs

            function onOpen() {
                  SpreadsheetApp.getUi()
                  .createMenu('Ordine')
                  .addItem('Apri Maschera', 'ApriSceltaCategoria')
                  .addToUi();
                  //doGet();
                  ApriSceltaCategoria();  
            }
            // Visualizza l'interfaccia grafica.
            // Chiamata alla funzione "setRngProdotto()" per assicurare che tutti i nuovi valori sono inclusi nella dropdown list quando l'interfaccia è visualizzata.
            function ApriSceltaCategoria() {
              var ss;
              var html;
              setRngCat();
              ss = SpreadsheetApp.getActiveSpreadsheet();
              html = HtmlService.createHtmlOutputFromFile('Scelta Categoria').setSandboxMode(HtmlService.SandboxMode.IFRAME);
              ss.show(html);
            }
            function ApriSceltaMarchio(CategoriaScelta) {
              var ss;
              var html;
              setRngMarchi();
              setRngCompleto();
              ss = SpreadsheetApp.getActiveSpreadsheet();
              html = HtmlService.createHtmlOutputFromFile('Scelta Marchio').setSandboxMode(HtmlService.SandboxMode.IFRAME);
              ss.show(html);
            }
            function ApriSceltaProdotto(ProdottoScelto) {
              var ss;
              var html;
              setRngProdotti();
              setRngCompleto();
              ss = SpreadsheetApp.getActiveSpreadsheet();
              html = HtmlService.createHtmlOutputFromFile('Scelta Prodotto').setSandboxMode(HtmlService.SandboxMode.IFRAME);
              ss.show(html);
            }
            // Chiamata dalla Client-side JavaScript nella pagina.
            // Usa l'argomento del nome del range per estrarre i valori. I valori sono quindi ordinati e restituiti come un array di arrays.
            function getValuesForRngName(rngName) {
              var rngValues = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(rngName).getValues();
              return rngValues.sort();
            }
            //Expand the range defined by the name as rows are added
            function setRngCat() {
              var ss = SpreadsheetApp.getActiveSpreadsheet();
              var sh = ss.getSheetByName('Prodotti');
              var firstCellAddr = 'F2';
              var dataRngRowCount = sh.getDataRange().getLastRow();
              var listRngAddr = (firstCellAddr + ':F' + dataRngRowCount);
              var listRng = sh.getRange(listRngAddr);
              ss.setNamedRange('rngListaCategorie', listRng);
            }
            //Expand the range defined by the name as rows are added
            function setRngMarchi() {
              var ss = SpreadsheetApp.getActiveSpreadsheet();
              var sh = ss.getSheetByName('Prodotti');
              var firstCellAddr = 'B2';
              var dataRngRowCount = sh.getDataRange().getLastRow();
              var listRngAddr = (firstCellAddr + ':B' + dataRngRowCount);
              var listRng = sh.getRange(listRngAddr);
              ss.setNamedRange('rngListaMarchi', listRng);
            }
            //Expand the range defined by the name as rows are added
            function setRngProdotto() {
              var ss = SpreadsheetApp.getActiveSpreadsheet();
              var sh = ss.getSheetByName('Prodotti');
              var firstCellAddr = 'C2';
              var dataRngRowCount = sh.getDataRange().getLastRow();
              var listRngAddr = (firstCellAddr + ':C' + dataRngRowCount);
              var listRng = sh.getRange(listRngAddr);
              ss.setNamedRange('rngListaProdotti', listRng);
            }
            //Expand the range defined by the name as rows are added
            function setRngCompleto() {
              var ss = SpreadsheetApp.getActiveSpreadsheet();
              var sh = ss.getSheetByName('Prodotti');
              var firstCellAddr = 'A2';
              var dataRngRowCount = sh.getDataRange().getLastRow();
              var listRngAddr = (firstCellAddr + ':I' + dataRngRowCount);
              var listRng = sh.getRange(listRngAddr);
              ss.setNamedRange('rngListaCompleta', listRng);
            }
            function OrdinaDati() {
              setRngCat();
              var rangeToSort = SpreadsheetApp.getActiveSpreadsheet().getRangeByName('rngListaCategorie').getValues();
              var selectionArray = rangeToSort.getValues(); 
              rangeToSort.setValues(selectionArray.sort(ordinaLi));
              setRngMarchi();
              var rangeToSort = SpreadsheetApp.getActiveSpreadsheet().getRangeByName('rngListaMarchi').getValues();
              var selectionArray = rangeToSort.getValues(); 
              rangeToSort.setValues(selectionArray.sort(ordinaLi));
              setRngProdotto();
              var rangeToSort = SpreadsheetApp.getActiveSpreadsheet().getRangeByName('rngListaProdotti').getValues();
              var selectionArray = rangeToSort.getValues(); 
              rangeToSort.setValues(selectionArray.sort(ordinaLi));
              setRngCompleto();
              var rangeToSort = SpreadsheetApp.getActiveSpreadsheet().getRangeByName('rngListaCompleta').getValues();
              var selectionArray = rangeToSort.getValues(); 
              rangeToSort.setValues(selectionArray.sort(ordinaLi));
            }
            function ordinaLi(a,b) {
              // Sorts on the first column in the selection ONLY, ascending order.
               a=a[0];
               b=b[0];
               return a==b?0:(a<b?-1:1);
            }
            /**
             * Get the URL for the Google Apps Script running as a WebApp.
             */
            function getScriptUrl() {
             var url = ScriptApp.getService().getUrl();
             return url;
            }

            /**
             * Get "home page", or a requested page.
             * Expects a 'page' parameter in querystring.
             *
             * @param {event} e Event passed to doGet, with querystring
             * @returns {String/html} Html to be served
             */
            function doGet(e) {
              //Logger.log( Utilities.jsonStringify(e) );
              Logger.log(e.parameter.page);
              var pgToLoad = e.parameter.page;

              if (!e.parameter.page) {
                Logger.log('!e.parameter.page')
                // When no specific page requested, return "home page"
                return HtmlService.createTemplateFromFile('Scelta Categoria').evaluate()
                   .setSandboxMode(HtmlService.SandboxMode.IFRAME);
              }
              Logger.log('there is something for the page');
              // else, use page parameter to pick an html file from the script
              return HtmlService.createTemplateFromFile('Scelta Categoria').evaluate()
                .setSandboxMode(HtmlService.SandboxMode.IFRAME);
            }

选择Category.html

        <!DOCTYPE html>
        <html style= "background-color:#B7CEEC;">
          <head>
            <base target="_top">
            <p id="p_cat">Categoria scelta: - </p>
          </head>
          <body>
        <title>TITOLO</title>
          <div>
            <form>
              <hr>
              <p style= "text-align: center;">Seleziona una <b style= "color:red;">CATEGORIA</b></p>
              <hr>
              <table id="Tabella">

              </table>
              <div style="text-align: center;">
                <input type="button" style="font-size: 14px;" id="btnAvanti" value="Avanti -->"
                  onclick= "google.script.run.withSuccessHandler(google.script.host.close).ApriSceltaMarchio()" />
              </div>
            </form>
          </div>
            <hr>
            <div style="float:right;">
              <input type="button" value="Chiudi"
                onclick="google.script.host.close()" />
            </div>
            <script type="text/javascript">
            function estraicategorie (values) {
              document.getElementById("btnAvanti").disabled = true;
              var categorie = eliminaduplicati(values);
              for (i = 0;i < categorie.length; i +=1) {
                  var tr = document.createElement('tr');   
                  var td1 = document.createElement('td');
                  var td2 = document.createElement('td');
                  var elemlabel = document.createElement('label');
                  strlabel = "" + categorie [i] + ""
                  elemlabel.innerHTML = strlabel.bold();
                  var eleminput = document.createElement('input');
                  eleminput.setAttribute('type', 'radio');
                  eleminput.setAttribute('name', 'categorie');
                  eleminput.setAttribute('value', categorie [i]);
                  td1.appendChild(elemlabel);
                  td2.appendChild(eleminput);
                  tr.appendChild(td1);
                  tr.appendChild(td2);
                  document.getElementById('Tabella').appendChild(tr);
              }
              var ch = document.getElementsByName('categorie');
              for (var i = ch.length; i--;) {
                ch[i].onchange = function() {
                  document.getElementById("btnAvanti").disabled = false;
                  document.getElementById("p_cat").innerHTML = "Categoria scelta: " + this.value;
                 }
               }    
            }
            function eliminaduplicati(a) {
              var temp = {};
              for (var i = 0; i < a.length; i++)
                temp[a[i]] = true;
                var r = [];
                for (var k in temp)
                  r.push(k);
                  return r;
            }
            function populate() {
              google.script.run.withSuccessHandler(estraicategorie).getValuesForRngName('rngListaCategorie');
            }
            </script>
            <script  type="text/javascript">
              // Using the "load" event to execute the function "populate"
              window.addEventListener('load', populate);
            </script>
          </body>
        </html>

Scelta marchio.html

            <!DOCTYPE html>
            <html style= "background-color:#B7CEEC;">
              <head>
                <base target="_top">
              <p id="p_cat"></p><p id="p_mar"></p>
              </head>
              <body>
            <title>TITOLO</title>
              <div>
                <form>
                  <hr>
                  <p style= "text-align: center;">Seleziona un <b style= "color:red;">MARCHIO</b></p>
                  <hr>
                  <table id="Tabella">

                  </table>
                  <div style="text-align: center;">
                    <input style="font-size: 14px;" type="submit" id="btnIndietro" value="<-- Torna>" onclick= "google.script.run.withSuccessHandler(google.script.host.close).ApriSceltaCategoria()" />   
                    <input style="font-size: 14px;" type="submit" id="btnAvanti" value="Avanti -->" onclick= "google.script.run.withSuccessHandler(google.script.host.close).ApriSceltaProdotto()" />   
                  </div>
                </form>
              </div>
                <hr>
                <div style="float:right;">
                  <input type="button" value="Chiudi" onclick="google.script.host.close()" />
                </div>
                <script type="text/javascript">
                function estraimarchi (values) {
                  document.getElementById("btnAvanti").disabled = true;
                  var categoria =              // HOW CAN I ACHIEVE THIS ??? 
                  var elencofiltratoXcat = [];
                  for (var i = 0; i < values.length; i +=1) {
                    if (values [i][5] === categoria) {
                      elencofiltratoXcat.push(values [i]);
                    }
                  }
                  var elencofiltratomarchi = [];
                  for (i = 0; i < elencofiltratoXcat.length; i +=1) {
                      elencofiltratomarchi.push(elencofiltratoXcat[i][1]);
                  }
                  var marchi = [];
                  marchi = eliminaduplicati(elencofiltratomarchi);
                  marchi.sort();
                  for (i = 0;i < marchi.length; i +=1) {
                      var tr = document.createElement('tr');   
                      var td1 = document.createElement('td');
                      var td2 = document.createElement('td');
                      var elemlabel = document.createElement('label');
                      strlabel = "" + marchi [i] + "";
                      elemlabel.innerHTML = strlabel.bold();
                      var eleminput = document.createElement('input');
                      eleminput.setAttribute('type', 'radio');
                      eleminput.setAttribute('name', 'marchi');
                      eleminput.setAttribute('value', marchi [i]);
                      td1.appendChild(elemlabel);
                      td2.appendChild(eleminput);
                      tr.appendChild(td1);
                      tr.appendChild(td2);
                      document.getElementById('Tabella').appendChild(tr);
                  }
                  var ch = document.getElementsByName('marchi');
                  for (var i = ch.length; i--;) {
                    ch[i].onchange = function() {
                      document.getElementById("btnAvanti").disabled = false;
                      document.getElementById("p_cat").innerHTML = "Categoria: " + categoria;
                      document.getElementById("p_mar").innerHTML = "Marchio: " + this.value;
                     }
                   }    
                }
                function populate(){
                  google.script.run.withSuccessHandler(estraimarchi).getValuesForRngName('rngListaCompleta');
                }
                function eliminaduplicati(a) {
                  var temp = {};
                  for (var i = 0; i < a.length; i++)
                    temp[a[i]] = true;
                    var r = [];
                    for (var k in temp)
                      r.push(k);
                      return r;
                }    
                </script>
                <script>
                  // Using the "load" event to execute the function "populate"
                  window.addEventListener('load', populate);
                </script>
              </body>
            </html>

正如我在代码中评论的那样,问题是当我尝试在对话框之间传递值时。我尝试在Scelta Categoria.html中使用

          document.getElementById("btnAvanti").disabled = false;
          document.getElementById("p_cat").innerHTML = "Categoria scelta: " + this.value;
          var userProperties = PropertiesService.getUserProperties();
          var userCat = userProperties.setProperty('SceltaCategoria', '' + this.value + '');

但无所事事。 当然我可以打开另一张纸并将用户选择放在A1中,然后隐藏纸张,但如果我可以使用变量,我会更快地报名。 任何建议,尝试保持在这种情况下而不是在网络应用程序中? 提前谢谢,抱歉我的英语不好。

0 个答案:

没有答案