最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I slice strings in Google Apps Script like in Python? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to make a Google Sheet that opens to an assigned sheet automatically for a large team using their gmail addresses when accessing it. How do I slice a string in Apps Scripts? The "email.slice" on line three is just something I made up as a place holder.

function onOpen() {
    var email = Session.getActiveUser().getEmail();
    var username = email.slice[0:-9];
    var ss= SpreadsheetApp.openById(username);
    SpreadsheetApp.setActiveSpreadsheet(ss);
}

I'm trying to make a Google Sheet that opens to an assigned sheet automatically for a large team using their gmail addresses when accessing it. How do I slice a string in Apps Scripts? The "email.slice" on line three is just something I made up as a place holder.

function onOpen() {
    var email = Session.getActiveUser().getEmail();
    var username = email.slice[0:-9];
    var ss= SpreadsheetApp.openById(username);
    SpreadsheetApp.setActiveSpreadsheet(ss);
}
Share Improve this question edited Jan 31, 2015 at 1:52 Alan Wells 31.3k16 gold badges111 silver badges162 bronze badges asked Jan 31, 2015 at 1:28 NonCreature0714NonCreature0714 6,01410 gold badges33 silver badges55 bronze badges 1
  • List of JavaScript string Methods: Microsoft Documentation Mozilla Documentation JavaScript String Reference List – Alan Wells Commented Jan 31, 2015 at 2:17
Add a ment  | 

4 Answers 4

Reset to default 5

The slice method returns part of the string. You could return all of it, but there's no point in that. There are two parameters, one is optional, the start and end parameters. Start es first, end is second, and end is optional. If the end parameter is not used, the method automatically goes to the end of the string.

Apps Script uses JavaScript, so any JavaScript reference material that you want to use will give you the answers for almost everything related to basic programming.

In your case, you need to bine slice with indexOf().

var username = email.slice(0, email.indexOf("@"));
Logger.log('username is: ' + username); //VIEW, LOGS to see print out

Substring will work as well

var username = email.substring(0, email.indexOf("@"));

Here is what I ended up doing:

function onOpen() {
    var email = Session.getActiveUser().getEmail();
    var ldap = email.slice(0,-11);
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName(ldap));
    }

Worked well.

It seems that the Splice function was reserved for Arrays and there is some issues applying it on Strings.

I have just created a small additional function that converts a String into an Array, makes the splice and then converts it back into a String:

function stringSplice(string,start,numElementsToDelete)
{
  var arr = [];
  var i;
  for(i = 0; i<string.length; i++) arr[i] = string[i];
  arr = arr.splice(start,numElementsToDelete)
  string = "";
  for(i = 0; i<arr.length; i++) string += arr[i];
  return string;
}

Then it works like a regular splice on Strings:

stringSplice("New_String",0,3);

Output: New

发布评论

评论列表(0)

  1. 暂无评论