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.
Related
Please Forgive me if this has been covered couldn't find it. But Does anyone have an idea on how to get a weekday (monday, Tues, Ect...) in this Script i have running.
function newColumn() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Song of the Day');
sheet.insertColumnAfter(1);
sheet.getRange("B1").setValue(new Date()).setNumberFormat('mmm/d/yyyy');
}
Currently ^ It makes a new column B and adds the date in cell B1 Every day I would just like that date to remain the same but also display the day of the week beside it also Thanks in advance!
Display Day of Week:
function DisplayDayOfWeek() {
Logger.log(["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][new Date().getDay()])
}
If I understand right you want Column B to contain the date, and you want that to format like 'mmm/d/yyyy' and then you want column C to also have the date but to be formatted to show the Name of the Day.
I would modify your script to place the date unformatted into both columns B and C, and then use the formatting controls in your spreadsheet to display them how you want to.
Your code would change to:
function newColumn() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Song of the Day');
sheet.insertColumnAfter(1);
sheet.getRange("B1").setValue(new Date());
sheet.insertColumnAfter(2);
sheet.getRange("C1").setValue(new Date());
}
On Your spreadsheet choose the Column C, then from Format>Custom Date and Time choose one with the Name of the Day listed, and remove the parts you don't want until it looks like this:
custom date image "Day Name Only"
If you wanted to format it with code then as shown here Javascript in Google Sheets script: help using setNumberFormat
you should grab the cell and format the cell:
var cell = sheet.getRange("B1");
cell.setNumberFormat('mmm/d/yyyy');
var cell = sheet.getRange("C1");
cell.setNumberFormat('dddd+');
Date number Formattings found here: https://developers.google.com/sheets/api/guides/formats
I see lots of for (var i + code written but I have never been able to understand it. I can set a formula but I dont know how to manipulate it.
My current struggle is that I want my script setFormula(=A1+B2+X) to work by grabbing a value and minusing 1 from it. As I understand it would probably end up as setFormula(=A1+B2+" + i + ")"
In my specific case, X can either be 1 of the 2 following (ideally the 2nd one) to be 24 (by getting the value of 25 and then minusing 1 from it). The date on the sheet is the 25th of the month is in cell A1, and the tab (sheet) name is xxx25. I need to be able to have X as 25-1: we want to end up with it printing =A1+B2+24 into the cell.
1: Day(A1)-1 (if the day in A1 was 25th of a month, this would give us the result of 24)
So in cell A1 on the current sheet it has DD MMMM YYYY so lets say if its the 25th of a month and year, it will set the formula in the sheet as =A1+B2+24
2: The tab (sheet) name is xxx25. This tab will always have 3 letters followed by the day of the month.
(In my case I have 1 for each day of the month, such as xxx1 xxx2 .. xxx9 xxx10. If this needs to be changed on my side to xxx01 xxx02 .. etc I can do that if required.)
So I guess this method would grab the last 2 values of the sheetname (the date, which in the example is 25th) so it extracts(?) it from xxx25 and now remembers 25, minuses 1 from it, and places it into the formula to become =A1+B2+24
Thanks, please let me know if any explanation more is needed.
Will this be a user-executed function that runs in the currently open sheet? If so, you could grab the active sheet's name and extract all but the first 3 characters from it:
function myFunc() {
let sheet = SpreadsheetApp.getActiveSheet();
let cell = sheet.getRange('C1'); // Replace with the desired cell
let sheetName = sheet.getName();
let onlyDate = sheetName.substring(3);
cell.setFormula(`=A1+B2+${onlyDate-1}`);
}
Using the value of A1:
Here I created a script that will create a custom menu. Whenever the menu is clicked, it will get the active range, use it to get the current sheet, get the value of A1, parse it and use its value in setFormula.
Code:
function onOpen() {
/*
This function will add a custom menu in Google Sheets
named Set Formula and will execute setFormula function
*/
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Set Formula', 'setFormula')
.addToUi();
}
function setFormula() {
var range = SpreadsheetApp.getActiveRange(); //get the active range
var sheet = range.getSheet(); //get the sheet of active range
var date = sheet.getRange('A1').getDisplayValue(); //get the value of A1
var day = date.split(" ")[0] //split using space and get the first element of array
range.setFormula(`=A1+B2+${day-1}`); //set formula to the active range
}
Demo:
References:
Custom Menu
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
}
I'm new to scripting, but need to do the following in Google sheet script for my RPG gaming system...
Check a cell for the highest one-digit number. i.e. 1 6 3 2 4 = 6 highest number.
Check the same in another cell.
Depending on the combo of these digits, i.e. 6-2, 3-4, 1-6 etc, text from a specific cell in a grid table on another sheet should be copied.
The grid has 1 to 6 vertically, and 1 to 6 horizontally, different texts in each combo cell.
The copied text shall be entered into a specific cell (i.e. E10) in the first sheet.
Can someone help me?
From what I see, you would like to check the highest value in two cell, and would like to use that to get an exact location in a table of values, and get that value printed on another cell.
The problem can be solved in 3 phases.
1) Finding the highest value in the cell.
2) Retrieving the value in the table.
3) Setting the destination cell with the retrieved value.
1) Depending on if you are finding the highest value in a cell refers to:
A) 1534 in one cell and taking 5
OR
B) 1 in A1, 5 in A2, 3 in A3 and 4 in A4. and retrieving 5 from A2 cell.
A: Your solution would use a combination of .split() & Array.prototype.concat.apply() & Math.max.apply(). Reference: Inputting an array into Math.Max in Google Apps Scripts
B: Your solution would use a combination of getRange(row,column,row,column) & Array.prototype.concat.apply() & Math.max.apply()
You would get an output of two max number, which we would call A and B
2) To retrieve the value, you would require the getRange(A,B) & getValue() and depending on where the table is you might need to modify A and B to get the correct location in the table.
3) To set the value in cell E1, you would require the setValue() function.
You might want to try resolving the issue by yourself using the handles above. They might not be the most efficient, but should work. Alternatively i have a sample script below for you. There is a flaw, whereby if the value input is larger than your table, it would not flag out an error. so either you limit the users input or you would create a way to flag out the error:)
function SetRetrievedValue() {
var app = SpreadsheetApp;
var ss = app.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//var cellcontent1 = sheet.getRange(2,1,6,1).getValues(); use this if its a range of cells you are searching
var cell1 = sheet.getRange(1,1).getDisplayValue(); //gets value as string
var cellcontent1 = cell1.split(""); // splits up the string individually
var newcellcontent1 = Array.prototype.concat.apply([], cellcontent1); // flatten the array
var maxNum1 = Math.max.apply(null, newcellcontent1); //gets the max value in the array
// repeat of cell 1
var cell2 = sheet.getRange(1,2).getDisplayValue();
var cellcontent2 = cell2.split("");
var newcellcontent2 = Array.prototype.concat.apply([], cellcontent2);
var maxNum2 = Math.max.apply(null, newcellcontent2);
var tablecell = ss.getSheetByName("Table sheet").getRange(maxNum1,maxNum2).getValue(); //retrieve the value based on the corresponding max value
sheet.getRange(1,3).setValue(tablecell); // sets the cell C1 as the value retrieved from the table
}
First of all, I'm new to coding - so the script that I have I combined it from a couple different scripts and it seems to work, but I would like to simplify it.
I have such a script:
function count() {
var Month = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Private').getRange('A2').getValues();
var PCC = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Private').getRange('B1').getValues();
var PCCQ = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(Month).getRange(PCC);
var PCCS = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(Month).getRange('B4').getValue();
var PhoneCC = GmailApp.search(PCCS);
PCCQ.setValue(PhoneCC.length);
}
What it does:
it takes the name of the sheet where I want the results from cell A2 of sheet Private.
it takes the cell where I want the result from cell B1 of sheet Private.
we get the range by combining the sheet name in 1 and cell from 2
it takes the query that we need from the sheet that get from 1 and cell B4
makes a search in Gmail with the query from 4
sets the value of the cell from point 3 as the length of the search from point 5
However I have a range of cells for point 2 that I need this to work through, let's say B1:B70.
Is there a way to adjust this script to do this?
Will be very thankful.
I'd make the following changes. Keep going.
var Month = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Private').getRange('A2').getValue();
var PCC = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Private').getRange('B1').getValue();
var PCCQ = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Month').getRange(PCC);//The range is not sensible unless it is a named range but the getRange does not work with named ranges.
var PCCS = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Month').getRange('B4').getValue();
var PhoneCC = GmailApp.search(PCCS); //how do you plan to display your results.