如何从数组中获取值以在输入字段中显示为文本

how to get value from array to display as text in input field

我正在使用 google sheet 以及数据和 AppScript 作为我的后端,我尝试了我的解决方案但不幸的是 none 它们的工作我正在尝试做的是从 Google 个工作表中获取数据,然后将其显示在输入字段中

这是从 Google 表格

获取数据的源代码
        var ss1 = "googleSheetId"
    function getData(id) {
        if (id == undefined)
        id = 14;
        var secondSS = SpreadsheetApp.openById(ss1);
        var secondSh = secondSS.getSheetByName('data');
        var Data = secondSh.getDataRange().getValues();
        Data.splice(0, 1);
        var tmpData = [];
        for (var x = 0; x < Data.length; x++) {
            if (Data[x][12] == id) {
                tmpData.push(Data[x]);
            }
        }
        var finalData = [];
        for (var x = 0; x < tmpData.length; x++) {
            for (var y = 4; y < tmpData[x].length; y++) {
                if (tmpData[x][y] != '') {
                    finalData[y - 3] = tmpData[x][y];
                }
            }
        }
        return { finalData }
    }

这是getData函数获取数据的源码 获取数据后,它看起来像这样

{finalData=[null, data1, Mon Mar 02 00:00:00 GMT+03:00 2020, data1@data1.com, null, data2, Mon Mar 02 00:00:00 GMT+03:00 2020, data3, data4, 14.0, null, yes, yes, null, null, null, Sun Aug 30 12:18:00 GMT+03:00 2020]}

我想做的是在原处获取数据并将其显示在输入字段中我正在处理的应用程序从脚本的 link 中获取订单号,然后通过它在函数中,依次获取数据,然后在输入字段中显示和显示每个信息用户将输入一个 link,如下所示:

https://script.google.com/a/herfy.com/macros/s/AKfycbwBrVDszfnKpewofnpweonfpwoenfwpeofnwpefn/dev```?page=report&request=100```

request=100 将获得行号 100 并将其传递给 getData 然后数据将显示在一个输入字段中或者在我的情况下不止一个输入字段

我使用 google.script.url.getLocation 从 link

获取数据
google.script.url.getLocation(request_data => {
        document.getElementById('request_breadcrumb').innerText = `request no ${request_data.parameter.request}`;
        document.getElementById('request_title').innerText = `request no ${request_data.parameter.request}`;
      })

我用的是企业版,没有使用内置的app脚本

描述

这里是一个使用 HTML 服务将数据从 Googles 电子表格获取到自定义对话框的简单示例。

您不能直接传输 Google App Script Date 对象,因此您需要在从 withSuccessHandler 回调函数返回之前对其进行字符串化。

Code.gs

function onOpen() {
  var menu = SpreadsheetApp.getUi().createMenu("Test");
  menu.addItem("Dialog","showDialog");
  menu.addToUi();
}

function showDialog() {
  try {
    var html = HtmlService.createTemplateFromFile('HTML_Demo').evaluate();
    SpreadsheetApp.getUi().showModalDialog(html,"Show Demo");
  }
  catch(err) {
    SpreadsheetApp.getUi().alert(err);
  }
}

function getCellA1() {
  try {
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
    var range = sheet.getRange("A1");
    return JSON.stringify({ a1: range.getValue() });
  }
  catch(err) {
    return err.message;
  }
}

HTML_Demo

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <input id="someText" type="text">
    <input id="someButton" type="button" value="Click Me" onclick="buttonOnClick()">
    <script>
      function buttonOnClick() {
        try {
          google.script.run.withSuccessHandler( 
            function(response) {
              alert("response = "+response);
              document.getElementById("someText").value = response;
            }
          ).getCellA1();
        }
        catch(err) {
          alert(err);
        }
      }
      function getValue(evt) {
        alert(evt.target);
      }
      (function () {
        document.getElementById("someText").addEventListener("change",getValue);
      }());
    </script>
  </body>
</html>

参考

经过多次尝试,我设法找到了问题的解决方案,并决定回答它以帮助他人

Google 应用脚本代码

var ss1 = "googleSheetId";
function getReportData(id){
  if(id == undefined)
    id = 14;
  var secondSS = SpreadsheetApp.openById(ss1);
  var secondSh = secondSS.getSheetByName('data');
  var Data = secondSh.getDataRange().getValues();
  var tmpData = [];
  for(var x=0 ; x<Data.length ; x++){
    if(Data[x][12] == storeId){
      tmpData.push(Data[x]);
    }
  }
  
  var finalData = [];
  for(var x=0 ; x<tmpData.length ; x++){
    for(var y=0 ; y<tmpData[x].length ; y++){
      if(tmpData[x][y] != ''){
//Here I add String the `String` object is used to 
//represent and manipulate a sequence of characters.
       finalData[y] = String(tmpData[x][y]);
       }
    }
  }
//Here I removed the parentheses
  return finalData;
}

这是从 getData 函数获取数据的源代码获取数据后,它看起来像这样,我正在使用 console.log(finalData); 因为你可以看到数据是否为 ​​null 它会有一个位置但会不能写成 null

[, 'data1', 'Mon Mar 02 00:00:00 GMT+03:00 2020', 'data1@data1.com', , 'data2', 'Mon Mar 02 00:00:00 GMT+03:00 2020', 'data3', 'data4', '14', , 'yes', 'yes', , , , 'Sun Aug 30 12:18:00 GMT+03:00 2020']

现在如果使用相同的 link 概念 https://script.google.com/a/herfy.com/macros/s/AKfycbwBrVDszfnKpewofnpweonfpwoenfwpeofnwpefn/dev?page=report&request=100

使用此代码,如果页面已加载,它将获得 request=100

document.addEventListener("DOMContentLoaded",function(){
    google.script.url.getLocation(request_data => {
      var id = request_data.parameter.request;
      google.script.run.withSuccessHandler(showReport).withFailureHandler(showError2).getReportData(storeId);
    })
});

显示报表功能


    function showReport(data) {
    /**
    * If data in column number 13 which is equal to array 12 is undefined or empty or null
    * Display alert else complete the sequence
    */
    if (data.at(12) == undefined || data.at(12) == " " || data.at(12) == null) {
        alertUser("danger", "No data try again later!")
        document.getElementById('disabled_form').style.display = "none";
        document.getElementById('enabled_form').style.display = "none";
    } else {
        /**
         * If data in column number 14 which is equal to array 13 is equal 'yes' or
         * data in column number 16 which is equal to array 15 is equal 'yes' show
         * the first model which is completely disabled else complete the sequence
         */
        if (data.at(13) == "yes" || data.at(15) == "yes") {
            document.getElementById('enabled_form').style.display = "none";
            document.getElementById('request_breadcrumb').innerText = `Request No ${data.at(12)}`;
            document.getElementById('date_and_time_of_report').value = data.at(0);
            document.getElementById('submitted_name_by').value = data.at(1);
            document.getElementById('mobile_number').value = data.at(2);
            document.getElementById('branch_number').value = data.at(3);
            document.getElementById('reporter_email').value = data.at(6);
            document.getElementById('preferred_language').value = data.at(7);
            document.getElementById('request_type').value = data.at(8);
            document.getElementById('expiration_date').value = data.at(9);
            document.getElementById('report_status').value = data.at(10);
            document.getElementById('email_was_sent_for_report_cancelled').value = data.at(13);
            document.getElementById('email_was_sent_stating_request_is_being_processed').value = data.at(14);
            document.getElementById('email_was_sent_the_request_has_been_executed_successfully').value = data.at(15);
            document.getElementById('note_on_request').value = data.at(16);
            document.getElementById('last_update_of_the_report').value = data.at(19);
            document.getElementById('email_of_editor').value = data.at(20);
            /**
             * display enabled model
             */
        } else {
            document.getElementById('disabled_form').style.display = "none";
            document.getElementById('request_breadcrumb').innerText = `Request No ${data.at(12)}`;
            document.getElementById('date_and_time_of_report_enabled_form').value = data.at(0);
            document.getElementById('submitted_name_by_enabled_form').value = data.at(1);
            document.getElementById('mobile_number_enabled_form').value = data.at(2);
            document.getElementById('branch_number_enabled_form').value = data.at(3);
            document.getElementById('reporter_email_enabled_form').value = data.at(6);
            document.getElementById('preferred_language_enabled_form').value = data.at(7);
            document.getElementById('request_type_enabled_form').value = data.at(8);
            document.getElementById('expiration_date_enabled_form').value = data.at(9);
            document.getElementById('report_status_enabled_form').value = data.at(10);
            document.getElementById('email_was_sent_stating_request_is_being_processed_enabled_form').value = data.at(14);
            document.getElementById('last_update_of_the_report_enabled_form').value = data.at(19);
        }
    }
}

显示错误函数

function showError(error){
    alert(error)
}

我使用的是企业版,我没有使用 built-in 应用程序脚本。

Live Demo

参考