Google App Script – Web App – 未以正确格式保存表单数据在Google表格中

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

Google App Script - Web App - Not saving forum data in Google Sheets in the correct format

问题

这是一个我创建的用于将提交保存到Google表格的Google App Script表单。

Code.gs

function doGet() {
  var template = HtmlService.createTemplateFromFile('Page');
  return template.evaluate();
}

function processForm(form) {
  var ss = SpreadsheetApp.openById('SPREADSHEET_ID');
  var sheet = ss.getSheetByName('Submissions');
  var ref = form.reference;
  var agentIds = form.agentIds.split(/[\s,]+/g).map(f => f.trim().replace(/[A-Z]/ig, "")).join(",");

  var values = sheet.getRange('B:B').getValues().flat();

  if (values.includes(ref)) {
    return "此参考编号已在先前的名单提交中使用过";
  } else {
    sheet.appendRow([new Date(), ref, agentIds]);
    return "谢谢!您的名单已成功提交";
  }
}

Page.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <form id="myForm">
      <label for="reference">课程参考号</label>
      <input type="text" name="reference" id="reference"><br><br>
      <label for="agentIds">代理人ID:</label>
      <textarea name="agentIds" id="agentIds" rows="10"></textarea><br><br>
      <input type="button" value="提交" onclick="submitForm()">
    </form>
    <div id="result"></div>
    <script>
      function submitForm() {
        var form = document.getElementById("myForm");
        google.script.run.withSuccessHandler(showResult).processForm(form);
      }
      
      function showResult(result) {
        document.getElementById("result").innerHTML = result;
      }
    </script>
  </body>
</html>

工作原理

该表单由两个字段组成。

  • 课程参考号(分配给每个课程的唯一编号)
  • 代理人/员工ID的文本区域

用户可以在文本区域中输入代理人ID时使用以下格式,包括可能的拼写错误,如额外的空格、逗号或三种格式的混合。

A) 用空格分隔。

A123456 B001234 TMP00123456

B) 用逗号分隔(可以包含或不包含空格)。

A123456,B001234, TMP00123456

C) 每行一个。

A123456
B001234
TMP00123456

问题

代理人ID保存为 12,345,600,123,400,100,000

期望的结果应为 123456,001234,00123456

当提交表单时,会发生以下情况...

  1. 正则表达式将清理代理人ID数据,使其输出值为 123456,001234,00123456 - 此部分存在问题。
  2. 脚本将检查是否已提交课程参考号。如果没有,它将继续。
  3. 时间戳、参考号和代理人ID现在已添加到Google表格中。
时间戳 参考号 代理人ID
2023年3月1日0:04:12 Class105 12,345,600,123,400,100,000

感谢您的帮助!

英文:

Here is a Google App Script Form that I created to save submissions into a Google Sheet.

Code.gs

function doGet() {
  var template = HtmlService.createTemplateFromFile(&#39;Page&#39;);
  return template.evaluate();
}

function processForm(form) {
  var ss = SpreadsheetApp.openById(&#39;SPREADSHEET_ID&#39;);
  var sheet = ss.getSheetByName(&#39;Submissions&#39;);
  var ref = form.reference;
  var agentIds = form.agentIds.split(/[\s,]+/g).map(f =&gt; f.trim().replace(/[A-Z]/ig, &quot;&quot;)).join(&quot;,&quot;);
  


  var values = sheet.getRange(&#39;B:B&#39;).getValues().flat();
  
  if (values.includes(ref)) {
    return &quot;This reference number has already been used for a previous roster submission&quot;;
  } else {
    sheet.appendRow([new Date(), ref, agentIds]);
    return &quot;Thanks! Your roster has been submitted successfully&quot;;
  }
}

Page.html

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;base target=&quot;_top&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form id=&quot;myForm&quot;&gt;
      &lt;label for=&quot;reference&quot;&gt;Class Reference #&lt;/label&gt;
      &lt;input type=&quot;text&quot; name=&quot;reference&quot; id=&quot;reference&quot;&gt;&lt;br&gt;&lt;br&gt;
      &lt;label for=&quot;agentIds&quot;&gt;Agent IDs:&lt;/label&gt;
      &lt;textarea name=&quot;agentIds&quot; id=&quot;agentIds&quot; rows=&quot;10&quot;&gt;&lt;/textarea&gt;&lt;br&gt;&lt;br&gt;
      &lt;input type=&quot;button&quot; value=&quot;Submit&quot; onclick=&quot;submitForm()&quot;&gt;
    &lt;/form&gt;
    &lt;div id=&quot;result&quot;&gt;&lt;/div&gt;
    &lt;script&gt;
      function submitForm() {
        var form = document.getElementById(&quot;myForm&quot;);
        google.script.run.withSuccessHandler(showResult).processForm(form);
      }
      
      function showResult(result) {
        document.getElementById(&quot;result&quot;).innerHTML = result;
      }
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

HOW IT WORKS

The form consists of 2 fields.

  • Class Reference # (A unique number assigned to each class)
  • Agent/Employee ID's Textarea

The users may use the following formats when entering the Agent ID's into the textarea, including typos such as extra spaces, commas, or a mixture of all 3 formats.

A) Separated by a space.

A123456 B001234 TMP00123456

B) Separated by a comma (with or without a space too)

A123456,B001234, TMP00123456

C) One / line.

A123456
B001234
TMP00123456

THE PROBLEM

The Agent ID's are being saved as 12,345,600,123,400,100,000

The expected results should be 123456,001234,00123456

When the form is submitted, the following occurs...

  1. REGEX will clean the Agent ID data so it has the output value of 123456,001234,00123456 - This part is broken.
  2. The script will check to see if the class reference has already been submitted. IF not, it will continue.
  3. The timestamp, Reference, and Agent ID's are now added to the Google Sheet.
Timestamp Reference # Agent IDs'
3/1/2023 0:04:12 Class105 12,345,600,123,400,100,000

Thanks for your help!

答案1

得分: 1

Pattern 1:

在这种模式下,用于放置 agentIds 的单元格的数字格式更改为文本。

从:

sheet.appendRow([new Date(), ref, agentIds]);

到:

var row = sheet.getLastRow() + 1;
sheet.getRange(row, 3).setNumberFormat("@"");
sheet.getRange(row, 1, 1, 3).setValues([[new Date(), ref, agentIds]]);

Pattern 2:

在这种模式下,在文本顶部添加一个单引号。

从:

var agentIds = form.agentIds.split(/[\s,]+/g).map(f => f.trim().replace(/[A-Z]/ig, "").join(",");

到:

var agentIds = "'" + form.agentIds.split(/[\s,]+/g).map(f => f.trim().replace(/[A-Z]/ig, "").join(",");

注意:

英文:

I think that the reason for your current issue is that the value is automatically formatted as a number value. In this case, how about the following 2 patterns?

Pattern 1:

In this pattern, the number format of the cell for putting agentIds is changed as text.

From:

sheet.appendRow([new Date(), ref, agentIds]);

To:

var row = sheet.getLastRow() + 1;
sheet.getRange(row, 3).setNumberFormat(&quot;@&quot;);
sheet.getRange(row, 1, 1, 3).setValues([[new Date(), ref, agentIds]]);

Pattern 2:

In this pattern, a single quote is added to the top of the text.

From:

var agentIds = form.agentIds.split(/[\s,]+/g).map(f =&gt; f.trim().replace(/[A-Z]/ig, &quot;&quot;)).join(&quot;,&quot;);

To:

var agentIds = &quot;&#39;&quot; + form.agentIds.split(/[\s,]+/g).map(f =&gt; f.trim().replace(/[A-Z]/ig, &quot;&quot;)).join(&quot;,&quot;);

Note:

huangapple
  • 本文由 发表于 2023年3月1日 16:15:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75601055.html
匿名

发表评论

匿名网友

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

确定