【问题标题】:How to create HTML Data entry form using Google spreadsheet as backend如何使用 Google 电子表格作为后端创建 HTML 数据输入表单
【发布时间】:2020-01-04 16:33:15
【问题描述】:

我有一个谷歌电子表格,其中一些行每天附加并使用谷歌电子表格,客户反馈团队跟进。

谷歌电子表格数据。

https://docs.google.com/spreadsheets/d/1V-XZdCUZAQVkfCat9vXVxITjjNMxNMPDin6B5j9uMWY/edit?usp=sharing

上面提到的谷歌电子表格在谷歌表格中总是有下面提到的数据(以蓝色突出显示):

参考编号 公司名称 联系人一号 联系方式 2 项目名称 代理编号

其余提到的详细信息将从基于用户响应的 HTML UI 捕获,最后单击“提交并下一步”或“下一步”,输入将存储在谷歌表格中。

用户必须首先在 HTML UI 上输入“Agent Id”,相应地,Ref ID 详细信息将被提供给特定的“Agent Id”用户。

如所附屏幕截图中所述,信息的左侧根据 googlespread 表是静态的,右侧信息将由用户根据电话交谈填写。

下面提到的细节将根据用户输入下拉或单选选项:

Product : Lite, Lite-I, Elite
Ref Code: LIT-1, LIT-2, LIT-3
Status  : Accept, Reject, Pending
Comment : Satisfied, Call Back, Pending

将得出以下详细信息:

Days Passed: It will be derived from the current system year - year mentioned in the `Date`

下面提到的细节将是用户输入的自由文本。

Client Name
Notes
Final_Status

笔记:将仅分配和显示代理人 Ref ID,其中 Agent ID 不为空且 Final_Status 为空或未在 Google 电子表格中标记为“提交并下一步”。

我们需要在 Google 电子表格中再添加一列,一旦 Final_Status 标记为“提交并下一步”或“下一步”,它就会根据系统日期捕获日期时间戳

只有在用户捕获了所有详细信息后,才会启用“提交和下一步”按钮。 Next 按钮只有在选择了Comment 选项时才会启用。

此外,如果 googlesheet 中没有可用的新行可用于使用 UI 的数据输入,则 UI 将通过单击“提交和下一步”或“下一步”按钮。

预期的用户界面:

【问题讨论】:

  • 你在哪里尝试这样做,解决你的问题很广泛问题?没有人会为您提供交钥匙解决方案以换取 50 个代表。
  • @tehhowch:嗯……我的错。
  • 您的问题似乎要求一个完整的业务解决方案,通常需要 $$$$ 才能获得并维护。请回顾一下什么是好问题:stackoverflow.com/help/how-to-ask
  • @tehhowch:哈哈哈......这只是一个课堂项目。
  • 你在浪费你的代表。至少包括您对解决方案的尝试以及您的尝试中是否存在特定问题。也许到那时,其他人将能够提供帮助。不管是否是课程项目,完成您请求的代码所需的时间将花费数小时。

标签: html r google-apps-script google-sheets google-forms


【解决方案1】:

看起来 Google Apps Developer 文档有一个类似的指南:

用于构建网络应用程序或在 Google 文档、表格和表单中添加自定义用户界面。

https://developers.google.com/apps-script/guides/html

看起来您需要授予 Google Apps 脚本访问权限,然后添加一个:

https://developers.google.com/apps-script/guides/standalone

您可以创建一个独立的脚本,或手动连接您的项目:

转到 Google 云端硬盘并单击新建 > 更多 > 连接更多应用程序。

当出现“将应用程序连接到云端硬盘”窗口时,在搜索框中键入“脚本”并按 Enter 键。

单击 Google Apps 脚本列表旁边的连接。

谷歌的脚本示例说你的脚本看起来像这样:

function onOpen() {
  SpreadsheetApp.getUi()
      .createMenu('Dialog')
      .addItem('Open', 'openDialog')
      .addToUi();
}

function openDialog() {
  var html = HtmlService.createHtmlOutputFromFile('Index');
  SpreadsheetApp.getUi() 
      .showModalDialog(html, 'Dialog title');
}

使用相应的 HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
   My Google Sheets Interface.
    <input type="button" value="Close"
        onclick="google.script.host.close()" />
  </body>
</html>

这里似乎有很好的文档:

https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app

所以我想说这是一个很好的起点。你想要实现的目标看起来确实可行,我认为这将是调整它以适应谷歌脚本支持的问题。

祝你好运!!

【讨论】:

    【解决方案2】:

    从电子表格上的标题信息创建的数据输入对话框

    代码.gs:

    function onOpen() {
      SpreadsheetApp.getUi().createMenu("My Menu")
      .addItem('Launch Dialog','launchTheFormAsDialog')
      .addToUi();
    }
    
    function buildForm() {
      var searchColumnName='RefId';
      var ss=SpreadsheetApp.getActive();
      var sh=ss.getSheetByName('Sheet1');
      var tA=sh.getRange(1,1,1,sh.getLastColumn()).getValues()[0];
      var hA=sh.getRange(2,1,1,sh.getLastColumn()).getValues()[0];
      tA.splice(1,5);
      var ftA=tA.slice();
      hA.splice(1,5);
      var fA=hA.slice();
      var dstr=Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "yyyy-MM-dd");
      var html='<style>input{margin:2px 5px 2px 0;}</style><form id="myForm">';
      for(var i=0;i<fA.length;i++) {
        switch(ftA[i]){
          case 'date':
            html+=Utilities.formatString('<br /><input type="%s" value="%s" name="%s" />&nbsp;%s',ftA[i],dstr,fA[i],fA[i]);
            break;
          default:
            html+=Utilities.formatString('<br /><input type="%s" name="%s" />&nbsp;%s',ftA[i],fA[i],fA[i]);
            break;  
        }
      }
      html+='<br /><input type="button" value="Submit" onclick="submitForm(this.parentNode)" /></form>';
      return {html:html};
    }
    
    function testUpload() {
      upload({'Status':'none', 'Comment':'to long to fit', 'ClientName':'Don Trump', 'RefCode':'Tweeter', 'Final_Status':'impeachment', 'Product':'Bullshit', 'RefId':'id3', 'DaysPassed':'12', 'Final_Status_Date':'2019-12-23', 'Date':'2019-12-23', 'Notes':'none'})
    }
    
    function upload(theForm) {
      Logger.log(theForm);
      var kA=Object.keys(theForm);
      kA.splice(kA.indexOf('refId'),1);//remove refID
      Logger.log(kA);
      var ss=SpreadsheetApp.getActive();
      var sh=ss.getSheetByName('Sheet1');
      var hA=sh.getRange(2,1,1,sh.getLastColumn()).getValues()[0];
      var hObj={};
      hA.forEach(function(e,i){hObj[e]=i+1});
      Logger.log(hObj);
      var vA=sh.getRange(3,1,sh.getLastRow()-2,2).getValues();
      for(var i=0;i<vA.length;i++) {
        if(theForm.RefId==vA[i][0]) {
          kA.forEach(function(key){
            Logger.log(hObj[key]);
            Logger.log(theForm[key]);
            sh.getRange(i+3,hObj[key]).setValue(theForm[key]);
          });
        }
      }
      return buildForm();
    }
    
    function launchTheFormAsDialog() {
      var ui=HtmlService.createHtmlOutputFromFile('theform').setHeight(550);
      SpreadsheetApp.getUi().showModelessDialog(ui, "Form Data Entry");  
    }
    

    表单.html:

    <!DOCTYPE html>
    <html>
      <head>
        <base target="_top">
        <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
        <script>
        $(function() {
       $(function(){
          google.script.run
          .withSuccessHandler(function(obj){
            $('#formDiv').html(obj.html);
          })
          .buildForm();
        });
        });
          function submitForm(frmData) {
            google.script.run
            .withSuccessHandler(function(obj){
              //console.log('flag1');
              $('#formDiv').html(obj.html);
            })
            .upload(frmData);
          }
          function updateSelect(vA,id){
            var id=id || 'sel1';
            var select = document.getElementById(id);
            select.options.length = 0; 
            for(var i=0;i<vA.length;i++) {
              select.options[i] = new Option(vA[i][1],vA[i][0]);
            }
          }
          console.log('My Code');
        </script>
      </head>
       <body>
        <h1 id="main-heading">Form Data Entry</h1>
        <div id="formDiv"></div>
    </body>
    </html>
    

    我的电子表格:

    我添加了输入数据类型,这样我就可以初始化日期字段,这样即使添加或移动了列,也可以完全根据电子表格上的数据构建表单。您始终可以隐藏该行,因为它位于顶部。

    对话框:

    【讨论】:

    • RefId 应该是唯一的,用户只需选择一次。另外,我可以显示公司名称,联系电话等信息吗?等。在同一屏幕的左侧。
    • 我不确定您所说的 refId 是什么意思应该是唯一的。我的印象是蓝色的信息将由您填写。您可以对 G 列左侧的所有内容执行任何操作。我正在使用 setValue() 写入 F 列右侧的每个单元格。
    • 例如,我是 AgentId = 12345 的用户,那么我应该只分配给那些与 id1 有关的行以进行数据输入。另外,如果我们可以针对每个唯一的 RefID 在 UI 左侧显示以蓝色突出显示的值,我需要您的建议。
    • 我已经上传了示例 UI 格式。请建议。
    • 所以 refId 是每个用户都已经知道的东西,所以我会把它改回文本。
    【解决方案3】:

    如果您熟悉 Angular 或 ReactJs 等客户端编码框架,最好的选择是将 Google 表格转换为 rest API 并从客户端应用程序中使用它。 Sheety 是最好的免费工具。

    否则,使用 Google Apps 脚本将表单链接到工作表。一切都在here一步一步解释。

    【讨论】:

      【解决方案4】:

      【讨论】:

        猜你喜欢
        • 2016-04-08
        • 1970-01-01
        • 2023-01-30
        • 1970-01-01
        • 1970-01-01
        • 2016-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多