function mySet() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
ss.getRange("J2").setFormula("=index(split(Drive!B2," "),1,2)");
}
I want to be able to have a space in between these to quotes.
ss.getRange("J2").setFormula("=index(split(Drive!B2,' '),1,2)");
This seems to work using ' ' However I Need " " for the formula to work.
You can also use single quotes outside the formula:
ss.getRange("J2").setFormula('=index(split(Drive!B2," "),1,2)');
Results after running your script:
Related
I have the following script which is required to remove characters in a string leaving only the characters which follow the ">" character. For example, "GOOD > WEEKEND". When the script runs the output should be "WEEKEND". As there is one space after ">" i'm using slice(v.indexOf(">")+2)
The problem is everytime the script runs it removes another two more characters. To get round this I'm trying to include an if function so that the characters will only be removed if ">" is detected in the string with regex. Any help will be appreciated to get it to run successfully. An example can be viewed at https://docs.google.com/spreadsheets/d/1xBBbwA9j3mcR3iBTekGPGil92c6iInKyYXcq6NQpgpg/edit?usp=sharing
Thank you
function UpdateZoneRate(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName('JOBS'); // adjust this to the name of your sheet
var zoneRate = sh.getRange('A2:A'+sh.getLastRow()).getValues();
for (var i=0;i<zoneRate.length;i++){
if (zoneRate[i][0].match(/([>])\w+/) )
var zoneValues = sh.getRange('A2:A'+sh.getLastRow()).getValues().flat().map(v=>[v.slice(v.indexOf(">")+2) || null])};
sh.getRange(2,53,zoneValues.length,1).setValues(zoneValues);
}
You can do that with a plain vanilla spreadsheet formula without resorting to scripting, like this:
=arrayformula( regexreplace(A2:A52, ".*> ?(.*)", "$1") )
To do the same with a script, try this:
function updateZoneRate() {
const range = SpreadsheetApp.getActive().getRange('JOBS!A2:A');
const zoneRate = range.getDisplayValues()
.flat()
.map(value => [value.replace(/.*> ?(.*)/, '$1')]);
range.offset(0, 1).setValues(zoneRate);
}
I am trying to delete all empty spaces in a specific column (currency column), because otherwise the imported values don't sum up correctly.
I tried this:
function cleaneuro() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('H2:H996').activate();
spreadsheet.getActiveRangeList().setNumberFormat('#,##0.00\\ [$€-1]');
spreadsheet.getActiveRangeList().replace(/\s/g, "") // here it should happen
spreadsheet.getRange('A2').activate();
};
But as so often, I seem to have a syntax error in the replace line...
any help?
You can't call replace() on a RangeList. You need to loop through the returned ranges instead and use a TextFinder.
function cleaneuro() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('H2:H996').activate();
spreadsheet.getActiveRangeList().setNumberFormat('#,##0.00\\ [$€-1]');
var ranges = spreadsheet.getActiveRangeList().getRanges();
for (var i = 0; i < ranges.length; i++) {
ranges[i].createTextFinder('\\s').useRegularExpression(true).replaceAllWith('');
}
spreadsheet.getRange('A2').activate();
};
The main reason you are not receiving the desired output is because the replace method you have used is typical for an object of type String and not of type RangeList.
Therefore, you can try getting the values from that range and you can also use this formula here in order to remove the empty spaces VALUE.split(' ').join('') instead for example
ranges[i].getValue().split(' ').join('')
Reference
Class Spreadsheet Apps Script - getActiveRangeList();
String.prototype.split().
I have a column in Google Sheets with strings.
Some of the rows start with an apostrophe ', some don't. But Google assumed that the apostrophe is because it's a text instead of letting it be part of the text.
How do I make it show these apostrophes?
You mentioned:
Some of the rows start with an apostrophe ', some don't...
How do I make it show these apostrophes?
By adding an extra apostrophe ' in the beginning of the cells.
EDIT
Following the comments by OP
That's inviable for 10000 rows.
and Liron
You can do that with "Find and replace" - search for ^' (caret and apostrophe) and replace it with '' (two apostrophes), and select "Search using regular expressions" and "Also search within formulas".
That would change every apostrophe at the beginning of a cell to two apostrophes.
I wrote the following script as an answer. I assume that you have a column with a mix of strings, some being enclosed in ' and some don't, like this example:
As shown on the picture, Sheets automatically hides the first '. To prevent this you have to add another ' at the start of the string. You can do it with this code:
function myFunction() {
var dataSheet = SpreadsheetApp.openById(
"{SPREADSHEET IDENTIFICATOR}").getSheets()[0];
var dataColumn = 2;
var dataRange = dataSheet.getRange(1, dataColumn, dataSheet.getLastRow());
var data = dataRange.getValues();
for (var i = 0; i < data.length; i++) {
for (var j = 0; j < data[i].length; j++) {
if (data[i][j].substr(-1, 1) == "'") {
data[i][j] = "''" + data[i][j];
}
}
}
dataRange.setValues(data);
}
The former code will first use a combination of .openById(), .getSheets(), .getRange() and .getValues() to open the spreadsheet, open the sheet, get the data range and read its values respectively. Then, the code will iterate over every value to check if it ends in a ', and if it does, the script will add an extra ' at the start of the string. The end result looks like this:
Please, ask me any question for additional help.
I have a formula in a cell (18,2) which is =(B17-B14)*B15+B17. So what you see if a numeric value. But when I tried to use the following script to fetch the value to a new cell, it put #num in the new cell. I tried other cells with formulas, when it is a simple formula, (b+c) for example, the getValue() works. But not this one. Why? Thank you for your help in advance!
function myfunction1 ()
{
var app=SpreadsheetApp;
var ss=app.getActiveSpreadsheet();
var activeSheet=ss.getActiveSheet();
var range = activeSheet.getRange(18,2);
var fstave=range.getValue();
// retrieve value
activeSheet.getRange(8,8).setValue(fstave);
Logger.log("Value: " + fstave);
}
You shold be able to use getDisplayValue() for the effect you want.
I have an apps script which takes email addresses from one spreadsheet from multiple cells and adds them them to another spreadsheet into 1 cell only.
Currently the email addresses are added to that cell and separated by a ", ".
I would like to add the email addresses into that same cell but add a new line after each address.
I know it is possible to have new lines in a cell when manually adding text, by typing CTRL-Enter.
How can this be achieved in apps script?
I have so far tried to append "\n" or "\r" or "\r\n" to the string, to no avail, so I have reverted my code back to adding ", " instead.
sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.appendRow(["Num Emails", "Emails"]);
var LMEmails = "";
var count = 0;
for (var i = 0; i < reviewers.LMEmails.length; i++) {
if (count) {
LMEmails += ", " + reviewers.LMEmails[i];
} else {
LMEmails += reviewers.LMEmails[i];
}
count++;
}
data = [count, LMEmails];
sheet.appendRow(data);
If anyone could help, I would very much appreciate it
Regards
Crouz
After searching for the same question, this was my solution:
var stringToDisplay = 'FirstBit' + String.fromCharCode(10) + 'SecondBit';
then
workSheet.getRange(1,1).setValue(stringToDisplay);
output will be in a single cell, :
FirstBit
SecondBit
Google Spreadsheets seems to ignore the new line if there are no characters after it.
Using "/n" works fine as long as you add at least one other character after it, this could be a space as shown below.
For example:
var myString = 'First Bit' + "\n " + "Second Bit";
It should be:
data = [[count, LMEmails]];