简体   繁体   中英

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

I'm using Google Scripts editor in correlation with Google sheets and forms. 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"

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. 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. (line 24, file "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. I have tried

username.toString(); 

before I call removeEmails, and even inside removeEmails but can't get rid of this error.

According to the docs , getSheetValues returns a 2-dimensional array, not a string. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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