Super tiny question

12 views
Skip to first unread message

Dave Wolovsky

unread,
Sep 16, 2023, 8:43:32 PMSep 16
to Google Apps Script Community
Thank you for reading!

What this function does (or is supposed to do) is take the input from a cell, split it by commas, and then throw those split inputs into a column of cells elsewhere.

The problem I have is this:

When people fill the original cell, they put a space after each comma (e.g. one, two, three...).

When it splits the inputs and puts them into other cells, "two" and "three" have a space before them. I want to remove that space.

Can anyone help?

function Formula() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getCurrentCell().setFormulaR1C1('=SPLIT(\'Strivings System Worksheet\'!R[-15]C[6],",")')
  .setFormulaR1C1('=SPLIT(\'Strivings System Worksheet\'!R[-15]C[7],",")');
  var destinationRange = spreadsheet.getActiveRange().offset(0, 0, 7);
  spreadsheet.getActiveRange().autoFill(destinationRange, SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
  spreadsheet.getCurrentCell().offset(0, 0, 7, 1).activate();
};

CBMServices Web

unread,
Sep 16, 2023, 8:58:17 PMSep 16
to google-apps-sc...@googlegroups.com
Look into the array method trim() it will remove spaces before and after characters.


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/8326206d-e835-427f-9e98-2305ebc19748n%40googlegroups.com.

Dave W

unread,
Sep 16, 2023, 9:31:11 PMSep 16
to google-apps-sc...@googlegroups.com
Thank you!

Someone else wrote this for me, and I'm a total noob.

Any ideas for how I would build Trim into this function so that it only takes away the 1st space of each output cell?


Dave Wolovsky

 



You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/o34a8pihrpg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAFX%2Bz3Xnp-k5UkLFxBpTtdnRMZQtguO4X9fzix%3D4VvZe%2BiwnAA%40mail.gmail.com.

Keith Andersen

unread,
Sep 16, 2023, 9:41:48 PMSep 16
to google-apps-sc...@googlegroups.com
function splitLoop() {
let ss = SpreadsheetApp.getActiveSpreadsheet();
let sht1 = ss.getSheetByName("Sheet1");

let cell_value = sht1.getRange("A1").getValue();//---get string value in cell
let split_cell = cell_value.toString().split(",");//---split by ","
let trimmed_values = [[]];
for(i = 0; i < split_cell.length; i++){ //loop through array values

// trim each value push to trimmed_values array [0]
trimmed_values[0].push(split_cell[i].trim());
}
//console.log(trimmed_values);//--- un-comment to test in IDE
//--- SHOULD LOOK LIKE THIS: [ [ 'one', 'two', 'three', 'four' ] ]
//--- set range as suit your needs (start row, start column, # of rows, # of columns)
sht1.getRange(1, 3, 1, trimmed_values[0].length).setValues(trimmed_values);

}




Keith Andersen

unread,
Sep 16, 2023, 9:45:50 PMSep 16
to google-apps-sc...@googlegroups.com

Dave W

unread,
Sep 16, 2023, 9:59:04 PMSep 16
to google-apps-sc...@googlegroups.com
Thank you so much Keith!

So after containing this in the sheet I want to apply it to, I use it as a formula, right?

I go to the destination cell and type in =splitloop(B7) if B7 is the origin cell?


Dave Wolovsky

 



Keith Andersen

unread,
Sep 16, 2023, 10:16:02 PMSep 16
to google-apps-sc...@googlegroups.com
No actually, that was a script function.

Try:
=ARRAYFORMULA(TRIM(SPLIT(A1, ",")))

Dave W

unread,
Sep 16, 2023, 10:26:26 PMSep 16
to google-apps-sc...@googlegroups.com

Keith Andersen

unread,
Sep 16, 2023, 10:55:52 PMSep 16
to google-apps-sc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages