我如何通过HTML表单数据的应用程序脚本变量? [英] How do I pass HTML form data to apps script variable?

查看:126
本文介绍了我如何通过HTML表单数据的应用程序脚本变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很难分配变量,我在一个HTML表单应用程序中使用脚本收集数据。我现在的code从HTML收集值和数据追加到在谷歌工作表中的一行。我想也做的是每个表单值赋值给一个变量,所以我可以发送自动电子邮件,等等...

我知道的,到目前为止...

HTML和脚本,但它从表单收集值,并将其发送到code.gs

功能addData开始像预期的那样值发送

表单数据附加到谷歌页

这两个code.gs记录器确认接收到的数据和接收的数据日志传送的值

尝试分配VAR columnA读取时,我得到一个执行错误:执行失败:类型错误:无法从不确定的读取属性0

下面是从HTML和GS文件以及​​在$ C $日志c.gs脚本我试图去上班......

非常感谢你提前...

的console.log:

[20141223,CraneJe960303,杰夫,鹤,1231231234,jkhj@jkhjk.com,123的任何圣这里,AR 11111,11111,19960303, 11-完成大三那年,16挣来的教育程度,L-领导力,37 **** 125,-85。**** 217,22]

code.js Logger.log:

Logger.log([接收到的数据:20141223,CraneJe960303,杰夫,起重机,1231231234,jkhj @ jkhjk.com,123条街在这里,AR 11111,11111,19960303,11完成的大三,16挣来的学士学位,L-领导,37 **** 125,-85。**** 217,22,[])[0秒]

 函数addData(数据){
  VAR SS = S preadsheetApp.openById('1g5N1 ****************** doOnE')getSheetByName('工作表Sheet1)。
  ss.appendRow(数据);
  Logger.log(确认接收到的数据:'+数据);  Logger.log('接收到的数据:'+数据);
变种columnA = data.values​​ [0];
  Logger.log('开始数据值:'+ columnA);
变种columnB = data.values​​ [1];
VAR等....


解决方案

变量数据看起来是一个数组。
通过将一个点运算符,字数据的结尾价值观,你试图调用名为数据的阵列上的方法。但对于数组名价值没有JavaScript方法。有一个 .valueOf 方法。

方法


  • CONCAT()连接两个或多个磁盘阵列,并返回加入的副本
    阵列

  • 的indexOf()搜索数组的元素,并返回其位置

  • 加入()加入一个数组的所有元素转换为字符串

  • lastIndexOf()搜索数组的元素开始,结束时,
    并返回其位置

  • pop()方法删除数组的最后一个元素,并返回该元素

  • 推()添加新的元素添加到数组的结尾,并返回新
    长度

  • 反向()反转数组中元素的顺序

  • 移()移除数组的第一个元素,并返回该
    元素

  • 片()选择一个阵列的一部分,并返回新的数组

  • 排序()排序数组中的元素

  • 拼接()添加从数组/删除元素

  • 的toString()数组转换为字符串,然后返回结果

  • 的unshift()添加新的元素添加到数组的开头,并返回
    新长度

  • 的valueOf()返回一个数组
  • 的原始值

属性


  • 构造函数返回创建函数Array对象的
    原型

  • 长度设置或返回数组
  • 元素的数量
  • 原型允许您的属性和方法添加到Array对象

如果你想在数组命名的数据索引的第一个值,可以使用:

 数据[0]

在换句话说:

  VAR columnA =数据[0];

如果它不是一个数组,但只是一个字符串,则可能需要将其转换为一个数组。

I am having a hard time assigning variables to data that I collect in a HTML form for use in Apps script. My current code collects values from HTML and appends the data to a row in a Google Sheet. What I would like to also do is assign each form value to a variable so I can send automated emails, etc...

What I know so far...

The HTML and script works, it collects values from the form and sends them to code.gs

function addData starts as expected as the values are transferred

The form data is appended to the Google Sheet

Both Code.gs loggers "Confirm received data" and "Received data" log the transferred values

I get an execution error when attempting to assign var columnA that reads: Execution failed: TypeError: Cannot read property "0" from undefined.

Below are logs from the html and gs files as well as the Code.gs script I am trying to get to work...

Thank you very much in advance...

console.log:

["20141223", "CraneJe960303", "Jeff", "Crane", "1231231234", "jkhj@jkhjk.com", "123 Any St Here, AR 11111", "11111", "19960303", "11-Completed Junior Year", "16-Earned Bachelors Degree", "L-Leadership", 37.****125, -85.****217, 22]

Code.js Logger.log:

Logger.log([Received data: 20141223,CraneJe960303,Jeff,Crane,1231231234,jkhj@jkhjk.com,123 Any St Here, AR 11111,11111,19960303,11-Completed Junior Year,16-Earned Bachelors Degree,L-Leadership,37.****125,-85.****217,22, []]) [0 seconds]

function addData(data) {
  var ss = SpreadsheetApp.openById('1g5N1******************doOnE').getSheetByName('Sheet1');
  ss.appendRow(data);
  Logger.log('Confirm received data: ' + data);

  Logger.log('Received data: ' + data);  
var columnA = data.values[0];
  Logger.log('Start data value: ' + columnA);
var columnB = data.values[1];
var etc....

解决方案

The variable data looks like it is an array. By putting a dot operator, and the word "values" on the end of data, you are trying to invoke a method on the array named "data". But there is no JavaScript method for an array name "values". There is a .valueOf method.

Methods:

  • concat() Joins two or more arrays, and returns a copy of the joined arrays
  • indexOf() Search the array for an element and returns its position
  • join() Joins all elements of an array into a string
  • lastIndexOf() Search the array for an element, starting at the end, and returns its position
  • pop() Removes the last element of an array, and returns that element
  • push() Adds new elements to the end of an array, and returns the new length
  • reverse() Reverses the order of the elements in an array
  • shift() Removes the first element of an array, and returns that element
  • slice() Selects a part of an array, and returns the new array
  • sort() Sorts the elements of an array
  • splice() Adds/Removes elements from an array
  • toString() Converts an array to a string, and returns the result
  • unshift() Adds new elements to the beginning of an array, and returns the new length
  • valueOf() Returns the primitive value of an array

Properties

  • constructor Returns the function that created the Array object's prototype
  • length Sets or returns the number of elements in an array
  • prototype Allows you to add properties and methods to an Array object

If you want the first value indexed in the array named data, use:

data[0]

In other words:

var columnA = data[0];

If it's not an array, but just a string, you may need to convert it to an array.

这篇关于我如何通过HTML表单数据的应用程序脚本变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆