从Google表格中检索值并填入Web应用程序输入。

huangapple go评论83阅读模式
英文:

Retrieve values from Google Sheet to Web App inputs

问题

I have a code that searches for a value entered in an INPUT field, and it looks for it on a Google sheet, and if the value exists, it returns a "correct" message, if the value does not exist, it returns an "incorrect" message ". What I am looking for is that the value entered in the INPUT field is searched on a Google sheet range of values, if the value entered in the INPUT field matches a value in the Google sheet range (column A), then show the "correct" message, and populate the fallowing INPUTS that correspond to the entered value. Example: If the name "Peter" is entered, when pressing "VALIDATE", the message "correct" should appear, and in the fields: ITEM, QUANTITY and AMOUNT, the values corresponding to the name entered in the INPUT should appear, in this case, the name "Peter" was entered so: ITEM = Item 1, QUANTITY = 25 and AMOUNT = $55.00, in the corresponding INPUT fields.

I know the line:
var user_name = sheetNames .getRange("A2").getValue();, should include the search range, in this case it would be "A2:D6", and change "getValue" to "getValues", but there I'm stuck. I don't know how to request that it search for a single value in the entire range of the Google sheet and how to make it extract the values from the line to which the entered name corresponds, and then pass them to the corresponding INPUTS on the Web App. All of this, using only JavaScript.

GS

function doGet() {
  var template = HtmlService.createTemplateFromFile("page1")
  return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function fetchUserValues1(){
    var ss = SpreadsheetApp.openByUrl("GOOGLE SHEET URL"); 
    var sheetNames = ss.getSheetByName("Sheet 1"); 
    var user_name  =  sheetNames.getRange("A2").getValue();
    return user_name;
}

HTML

<!DOCTYPE html>
<html>

<head>
    <base target="_top">
</head>
<br>
NAME:<br>
<input type="text" id="name">

<script>
    function checkUser(user_name) {
        if (document.getElementById("name").value == user_name) {
            alert("correct");
        }
        else {
            alert("incorrect");
        }
    }

    function handleButton() {
        google.script.run.withSuccessHandler(checkUser).fetchUserValues1()
    }
</script>
<br>
<br>
<input type='button' value='VALIDATE' onclick="handleButton()">
<br>
<br>
<br>
ITEM:<br>
<input type="text" id="Item"><br>
QUANTITY:<br>
<input type="text" id="Quantity"><br>
AMOUNT:<br>
<input type="text" id="Amount">

</html>

从Google表格中检索值并填入Web应用程序输入。

英文:

I have a code that searches for a value entered in an INPUT field, and it looks for it on a Google sheet, and if the value exists, it returns a "correct" message, if the value does not exist, it returns an "incorrect" message ". What I am looking for is that the value entered in the INPUT field is searched on a Google sheet range of values, if the value entered in the INPUT field matches a value in the Google sheet range (column A), then show the "correct" message, and populate the fallowing INPUTS that correspond to the entered value. Example: If the name "Peter" is entered, when pressing "VALIDATE", the message "correct" should appear, and in the fields: ITEM, QUANTITY and AMOUNT, the values corresponding to the name entered in the INPUT should appear, in In this case, the name "Peter" was entered so: ITEM = Item 1, QUANTITY = 25 and AMOUNT = $55.00, in the corresponding INPUT fields.
I know the line:
var user_name = sheetNames .getRange(&quot;A2&quot;).getValue();, should include the search range, in this case it would be "A2:D6", and change "getValue" to "getValues", but there I'm stuck. I don't know how to request that it search for a single value in the entire range of the google sheet and how to make it extract the values from the line to which the entered name corresponds, and then pass them to the corresponding INPUTS on the Web App. All of this, using only JavaScript.

GS

 function doGet() {
  var template = HtmlService.createTemplateFromFile(&quot;page1&quot;)
  return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
  }

function fetchUserValues1(){
    var ss = SpreadsheetApp.openByUrl(&quot;GOOGLE SHEET URL&quot;); 
     var sheetNames = ss.getSheetByName(&quot;Sheet 1&quot;); 
     var user_name  =  sheetNames .getRange(&quot;A2&quot;).getValue();
    return user_name;
}

HTML

&lt;!DOCTYPE html&gt;
&lt;html&gt;

&lt;head&gt;
    &lt;base target=&quot;_top&quot;&gt;
&lt;/head&gt;
&lt;br&gt;
NAME:&lt;br&gt;
&lt;input type=&quot;text&quot; id=&quot;name&quot;&gt;

&lt;script&gt;
    function checkUser(user_name) {
        if (document.getElementById(&quot;name&quot;).value == user_name) {
            alert(&quot;correct&quot;);
        }
        else {
            alert(&quot;incorrect&quot;);
        }
    }

    function handleButton() {
        google.script.run.withSuccessHandler(checkUser).fetchUserValues1()
    }
&lt;/script&gt;
&lt;br&gt;
&lt;br&gt;
&lt;input type=&#39;button&#39; value=&#39;VALIDATE&#39; onclick=&quot;handleButton()&quot;&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
ITEM:&lt;br&gt;
&lt;input type=&quot;text&quot; id=&quot;Item&quot;&gt;&lt;br&gt;
QUANTITY:&lt;br&gt;
&lt;input type=&quot;text&quot; id=&quot;Quantity&quot;&gt;&lt;br&gt;
AMOUNT:&lt;br&gt;
&lt;input type=&quot;text&quot; id=&quot;Amount&quot;&gt;

&lt;/html&gt;

从Google表格中检索值并填入Web应用程序输入。

答案1

得分: 3

在你的脚本中,以下是可能的修改:

Google Apps Script 部分:

function fetchUserValues1(v) {
  var ss = SpreadsheetApp.openByUrl("GOOGLE SHEET URL"); 
  var sheet = ss.getSheetByName("Sheet 1"); 
  var r = sheet.getRange("A2:A" + sheet.getLastRow()).createTextFinder(v).matchEntireCell(true).findNext();
  var col = sheet.getLastColumn();
  return r ? r.offset(0, 0, 1, col).getValues()[0] : Array(col).fill("");
}

HTML 和 JavaScript 部分:

<!DOCTYPE html>
<html>

<head>
  <base target="_top">
</head>

<body>
  <br>
  NAME:<br>
  <input type="text" id="name">
  <br>
  <br>
  <input type='button' value='VALIDATE' onclick="handleButton()">
  <br>
  <br>
  <br>
  ITEM:<br>
  <input type="text" id="Item"><br>
  QUANTITY:<br>
  <input type="text" id="Quantity"><br>
  AMOUNT:<br>
  <input type="text" id="Amount">
</body>

<script>
function checkUser(values) {
  document.getElementById("Item").value = values[1];
  document.getElementById("Quantity").value = values[2];
  document.getElementById("Amount").value = values[3];
  if (!values.every(e => !e)) {
    alert("correct");
  } else {
    alert("incorrect");
  }
}

function handleButton() {
  const value = document.getElementById("name").value;
  google.script.run.withSuccessHandler(checkUser).fetchUserValues1(value);
}
</script>

</html>

在这个脚本中,当name的值被找到时,将从列"B"到"D"的值放入输入标签中。当name的值未找到时,将从列"B"到"D"的空值放入输入标签中。

英文:

In your script, how about the following modification?

Google Apps Script side:

function fetchUserValues1(v) {
  var ss = SpreadsheetApp.openByUrl(&quot;GOOGLE SHEET URL&quot;); 
  var sheetNames = ss.getSheetByName(&quot;Sheet 1&quot;); 
  var r = sheet.getRange(&quot;A2:A&quot; + sheet.getLastRow()).createTextFinder(v).matchEntireCell(true).findNext();
  var col = sheet.getLastColumn();
  return r ? r.offset(0, 0, 1, col).getValues()[0] : Array(col).fill(&quot;&quot;);
}

HTML & Javascript side:

&lt;!DOCTYPE html&gt;
&lt;html&gt;

&lt;head&gt;
  &lt;base target=&quot;_top&quot;&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;br&gt;
NAME:&lt;br&gt;
  &lt;input type=&quot;text&quot; id=&quot;name&quot;&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;input type=&#39;button&#39; value=&#39;VALIDATE&#39; onclick=&quot;handleButton()&quot;&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
ITEM:&lt;br&gt;
  &lt;input type=&quot;text&quot; id=&quot;Item&quot;&gt;&lt;br&gt;
QUANTITY:&lt;br&gt;
  &lt;input type=&quot;text&quot; id=&quot;Quantity&quot;&gt;&lt;br&gt;
AMOUNT:&lt;br&gt;
  &lt;input type=&quot;text&quot; id=&quot;Amount&quot;&gt;
&lt;/body&gt;

&lt;script&gt;
function checkUser(values) {
  document.getElementById(&quot;Item&quot;).value = values[1];
  document.getElementById(&quot;Quantity&quot;).value = values[2];
  document.getElementById(&quot;Amount&quot;).value = values[3];
  if (!values.every(e =&gt; !e)) {
    alert(&quot;correct&quot;);
  } else {
    alert(&quot;incorrect&quot;);
  }
}

function handleButton() {
  const value = document.getElementById(&quot;name&quot;).value;
  google.script.run.withSuccessHandler(checkUser).fetchUserValues1(value);
}
&lt;/script&gt;

&lt;/html&gt;
  • In this script, when the value of name is found, the values from column "B" to "D" are put into the input tags. When the value of name is not found, the empty values from column "B" to "D" are put into the input tags.

Note:

References:

huangapple
  • 本文由 发表于 2023年1月9日 06:25:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定