简体   繁体   English

输入错误:在Google脚本/ JavaScript中将用户名与电子邮件地址分开

[英]Type Error : Separating username from email address in Google Scripts/JavaScript

I'm using Google Scripts editor in correlation with Google sheets and forms. 我正在使用Google Scripts编辑器与Google表格和表单相关联。 There is a form that logs a user's email address and stores it inside the sheet. 有一个表单记录用户的电子邮件地址并将其存储在工作表中。

I have used: 我用过:

var username = sheet.getSheetValues(numRows, 2, 1, 1); //getting username

this returns an object "testuser@domain.com" which I then send to a function titled "removeEmail" 这将返回一个对象“testuser@domain.com”,然后我将其发送到标题为“removeEmail”的函数

var newName = removeEmail(username);

... ...

function removeEmail(input) 
{
  var name =input;

  var stringLength = name.length;

  var sub = name.substring(0,stringLength-10);
  Logger.log(sub);

  //return sub;

}

When I created this function I had a defined email to test with, that lived inside the removeEmails function. 当我创建这个函数时,我有一个定义的电子邮件来测试,它存在于removeEmails函数中。 The logger showed that it was removing the domain name perfectly. 记录器显示它正在完全删除域名。 However, when I implemented this inside a bigger main function the function errors out. 但是,当我在一个更大的主函数中实现它时,函数会出错。 The message is: 消息是:

TypeError: Cannot find function substring in object testuser@domain.com. TypeError:无法在对象testuser@domain.com中找到函数子字符串。 (line 24, file "removeEmail") (第24行,文件“removeEmail”)

I believe this is because my function removeEmails is meant for strings, as it uses the string library, but the way I grab the data from the sheet doesn't store the variable as a string. 我相信这是因为我的函数removeEmails用于字符串,因为它使用字符串库,但我从工作表中获取数据的方式不会将变量存储为字符串。 I have tried 我努力了

username.toString(); 

before I call removeEmails, and even inside removeEmails but can't get rid of this error. 在我调用removeEmails之前,甚至在removeEmails里面但是无法摆脱这个错误。

According to the docs , getSheetValues returns a 2-dimensional array, not a string. 根据文档 ,getSheetValues返回一个二维数组,而不是一个字符串。 Try getting the username like this: 尝试获取这样的用户名:

var values = sheet.getSheetValues(numRows, 2, 1, 1); //getting username
var username = values[0][0]; // first row, first column since we're requesting just 1 cell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM