I need to add a $ between a "A1notation result" , it returns the possition of today´s date in an array of dates (row)
So I have a row (A1:AA1) with dates and I want to find today´s date to insert a formula
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("DB");
var date = Utilities.formatDate(now, 'GMT-5', 'dd/MM/yyyy');
var ranges = sheet.createTextFinder(date).findAll().map(r => r.getA1Notation());
sheet.getRangeList(ranges).activate();
Logger.log(ranges)
This will show where today´s date is found and log [T1](for example). I am using it to insert a formula and copyTo the destination in the same column to getLastRow(), but when used it changes the value to T2, T3 and so on instead of keeping the value in T1, so im trying to change it to $T$1 to keep it in the exact same cell and not moving downwards. Does this meake sense? Thanks in advanced
I don't think there's an option to anchor the cell automatically, surely with some string manipulation. I suggest you put in your formula INDIRECT("T1") and it won't move
Related
So, I've been trying to figure out a way to work on an individual cell of a given range, but I can't see how I can do this.
For instance, if I have set a variable to the range "A5:H25" and I want to change the values of one (or more) cells withing this range, how can one do this, keeping the other cell's values?
I know we can use an array of arrays to change multiple values, but what about the other values I want to keep untouched?
In my specific example, I have a range set this way:
var date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
var sprdsht = SpreadsheetApp.getActiveSpreadsheet();
var shtSessions = sprdsht.getSheets()[3];
var shtSessionslastRow = shtSessions.getLastRow();
var shtSessionslastCol = shtSessions.getLastColumn();
//Get table Ranges from the 4 sheets
var tblPushRange = shtPush.getRange(1, 1, shtPushlastRow, shtPushlastCol);
var tblSessionsRange = tblPushRange;
if (tblSessionsRange(1,1).getValue() == ""){ //<--- Check if 1st cell of the given range is empty
tblSessionsRange.setValues([[date]]) //<-- Set that 1st cell value to date
}else{
tblSessionsRange.offset(tblPushRange.getLastRow() + 1,1).setValue(date);
Logger.log(tblPushRange.getLastRow() + 1)
}
So, how can I change values in individual cells and keep all the other values untouched, within this range I set?
Note: I think the code in the else block isn't correct either, but I'll address it later!
Thanks
Psy
Solution:
You can use getCell(row,column) on a range to get a specific single cell. Note that the first cell in the range is designated as (1,1).
Sample:
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A5:H25");
// set value of cell B7 to "A"
range.getCell(3,2).setValue("A");
Reference:
Class Range | getCell()
I have a spreadsheet where I'm trying to add an automatic search function whenever a cell on the Sheet "List" Column 1 gets selected.
It's sort of like using a VLOOKUP function of the sheets but I've been unsuccessful trying to come up with an effective code for it.
Test Spreadsheet link
I made a simple test spreadsheet hopefully for people to easily understand how the sheet should work.
In the 'Database' sheet, there's a list of people with the fruits they like or dislike.
In the 'List' sheet, the list is reversed where the fruits are now the main list but here, when a person clicks on the cells with the fruits, the top cells should automatically update with the combined names of people who like or dislike that certain fruit.
The function should go like this:
If I click a cell on column 1 of the 'List' sheet,
A1 cell updates it's value with the active cell value
B1 cell updates it's value with the combined values based on who likes it from the 'Database' sheet Column 2.
B2 cell updates it's value similar as above but from Dislikes in Column 3
I've attached the code I currently have but I'm not able to get the last 2 steps working.
There is also a guide in the sheets on what the correct answers should look like for each fruit.
I got stuck looking for a way to make the steps 2 and 3 work. I was able to find a code here but it stops after finding one match. stackoverflow.com/questions/10838294/… My current problem is: Since the cells [B2:B] to filter from on the Database sheet has multiple values, is it possible to find all cells with at least a partial match, get the values of the cells on the left of that then list them into the B1 cell on the "List" sheet?
Test Spreadsheet link
Code.gs
function onSelectionChange(e){
var currentsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var currentcell = currentsheet.getActiveCell();
var activerow = currentcell.getRow();
var activecol = currentcell.getColumn();
var sname = currentsheet.getName();
// Check if current sheet name is correct
if (
sname == 'List'
)
{
// Check if this on first column
if (activecol == 1)
{
// ACTION
var name = currentcell.getValue(); //Get current cell for searching
var prefsheet = SpreadsheetApp.getActive().getSheetByName("Database");
var last = prefsheet.getLastRow();
var data = prefsheet.getRange(1,1,last,2).getValues(); // create an array of data from columns A and B
for(nn=0;nn<data.length;++nn){
if (data[nn][1]==name){break} ;
SpreadsheetApp.getActiveSheet().getRange('A1').setValue('Fruit: ' + name);
SpreadsheetApp.getActiveSheet().getRange('B1').setValue('Like: ' + data[nn][0]);
}
}
};
};
I was able to find a workaround for this and it turns out I didn't have to use a Google Script code to keep updating the formula on List!B1.
I'm new with scripting and sheets but I'll be sharing this formula here in case someone needs something like this in the future.
This is what I used on List!B1
=JOIN(", ",query(filter(Database!A2:B,REGEXMATCH(Database!B2:B,A1)),"Select Col1"))
Regexmatch does the search for partial value and returns as "TRUE"
Filter will then list these rows and query "Select Col1" will only keep the first column of the results and remove the second column. Lastly, Join formula will concatenate them into one cell and add separators.
Which is working very nicely for my purpose. If anyone has any suggestions I'd love to hear about it too.
I have shared below link of my sheet.
I tried every possible script to accomplish below task in the end in frustration i wipe out my whole script.
I would like to match text from column A and return or getValue of corresponding B column.
So I can use that getValue from its corresponding B column for further arithmetic operations.
Thank you.
sheet link - https://docs.google.com/spreadsheets/d/1SwYYacz9A9s6ZXrL44KtRN7gqnwXkhu4cDILdUA1iJQ/edit?usp=sharing
Basic steps:
Retrieve your data range
Form an 1D array out of your column A entries, e.g. with map()
Check either the search string is contained in the array - and if yes retrieve its position - e.g. with indexOf()
Retrieve the value with the respective row index in column B
Sample:
function myFunction() {
var matchText = "C";
var values = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange().getValues();
var columnA =values.map(function(e){return e[0]});
var row = columnA.indexOf(matchText);
if (row >= 0){
var Bvalue = values[row][1];
Logger.log(Bvalue);
}
}
I encourage you to take some time to study Apps Script, so you cannot only understand this code and adapt to your needs, but also write your own scripts.
I am learning coding in Sheets, and have already jigsawed together a couple of games (based off very limited coding knowledge. Very fun to see it come together
Next task:
I am logging my hours spent doing "Growth" things (like learning, exercise, work etc.) vs "Leisure" things (like TV, games, FB scrolling etc). Up until now I just scroll down to today's date and add in the numbers manually. Would be fun to have a button at the top of my sheet that just adds hours to the "Growth" or "Leisure" cells for today.
I have used formulae in the cell G371 to reveal the A1 notation of my target cell, hoping I can use this info in a script to write to that cell:
=substitute(Cell("address",vlookup(A1,A2:D367,3)),"$","")
Where cell A1 contains =today()
and A2:A367 are all the dates in the year.
and columns C and D hold numbers for growth and leisure respectively.
Then I tried this code to try to see how to write to a cell. I realise I am probably missing some very fundamental knowledge...
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cellname = ss.getRange('G371') // makes cellname refer to G371
var input = cellname.getValue() // makes 'input' return the contents of G371, in today's case its 'C120'
var test = ss.getRange(input) // hopefully reads 'C120' instead of "input"
test.setValue(1) // setting value to 1 for now, as I'm just testing cell
// referencing. later I will work on figuring out the
// math for the buttons. I'm not there yet.
So it didn't work. apparently the problem is with ss.getRange(input) but I don't know what to do about that.
So my question is how to write to a cell that is named in another cell?
hope it makes sense
Thanks!
PS Here is a copy of the sheet
https://docs.google.com/spreadsheets/d/1KkoCb8kY1XICMeB9bx65HXsldCS-fa75xJCaU52WGg8/edit?usp=sharing
The formula in the cell might be messing with the value you retrieve. To make sure that does not happen, you can use getDisplayValue() instead of getValue().
Another way:
Also, if you just want to retrieve the cell where to write today's growth (or leisure), you don't need to follow this roundabout way of creating a formula in another cell to reference this one. You can do this instead (check comments):
function todayGrowthCell() {
var sheet = SpreadsheetApp.getActiveSheet();
var date = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "d/M"); // Format today's date as in spreadsheet, in order to compare
var firstRow = 2; // Row where days start
var column = 3; // Column C (Growth)
var dates = sheet.getRange(2, 1, 366).getDisplayValues().map(row => row[0]); // Retrieve columns with dates (its display value, in order to compare with today's date, which is formatted accordingly)
var row = dates.indexOf(date) + firstRow; // Today's row
var cell = sheet.getRange(row, column); // Today's growth cell
cell.setValue(1); // Set value to today's growth cell
}
New to Google Apps Script, but have some VBA coding experience.
I am looking to be able to have the numeric format of a cell decided based on a different cell input.
eg.
Col A - Client Name (Validated range)
Col B - Currency Type (GBP, USD, EUR - validated list)
Col C - Fee (100 - free type by user)
Based on the selection in Col B it needs to put the format in col C as either:
£100.00 or
$100.00 or
€100.00.
This should be done in such a way that the "autosum" function displayed on the bottom of google sheet is still able to add the values, or if using a sumif function will result in the USD, GBP and EUR totals being correct. I.E. it cannot be a text based solution or at least not one where this is not the result.
Try using onEdit trigger, and set custom number format.
Here's code sampe for currency formats:
function customNumberFormat() {
var SS = SpreadsheetApp.getActiveSpreadsheet();
var ss = SS.getSheetByName('Sheet1');
var range1 = ss.getRange("C2");
var range2 = ss.getRange("C3");
var range3 = ss.getRange("C4");
var format1 = "£ 00.00"
var format2 = "$ 00.00"
var format3 = "€ 00.00"
range1.setNumberFormat(format1);
range2.setNumberFormat(format2);
range3.setNumberFormat(format3);
}
Make script to check current entry in column with sum and format it in proper currency.
But text solution with formula looks better for me:
This way you are hiding the entries in column with sum, and show text with format. Sums can be used in further formulas.
There's also simple ArrayFormula:
=ArrayFormula(TEXT(C2,VLOOKUP(OFFSET(B2,,,COUNTA(B2:B)),H:I,2,0)&" 0.00"))
If you paste it in D2, it'll expand automatically.