How do I set a column to display the name of the month instead of the whole date using the script?
The function can manually be performed by selecting Format>Number>August in the menu but I want to do this in code.
This is what I've tried but I can't seem to find what I'm supposed to put in as the "format condition":
function myFunction() {
var spr = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spr.getSheets()[0];
var column = sheet.getRange("A:A");
column.setNumberFormats("Month"); // I'm pretty sure it's not supposed to be "Month" here, so what is it supposed to be?
}
Clarification; I want to set an entire column to change: 01/01/2015 to show January.
The error message I get on above code is "Cannot find method setNumberFormats(string)" and it's the same if i change "Month" to "L" or "MM".
A lot of typos in your script...(please be more careful when writing code and make use of autocomplete to check spelling : control+SPACE)
code goes like this :
function myFunction() {
var spr = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spr.getSheets()[0];
var column = sheet.getRange("A:A");
column.setNumberFormat("MMMM"); // MMM for abbreviated month, MM for month number
}
Related
I have a google sheet with a list of people's initials in range A4:A.
These initials are each hyperlinked to a different part of my sheet.
Column MM contains many cells that all have a data validation dropdown list referencing the initials list in A4:A
I need a script that triggers on edit so that when a user selects initials in the dropdown list in column MM, the script replaces it with the hyperlinked version from column A
My scripting knowledge is rudimentary. Normally I'd do a replace something like the below but I'm not sure how to implement the search so it takes the value from column MM and searches for it in columnA (or even if this would take the hyperlink function along with it.
`function replaceText(){
var oldText = "AB";
var newText = "AB hyperlinked";
var sheet = SpreadsheetApp.getActive().getSheetByName('SHOTS');
sheet.getRange("MM1:MM" + sheet.getLastRow()).createTextFinder(oldText).replaceAllWith(newText);
}`
Thanks in advance for any help given
If you have the chance of having an additional column, you can do a workaround without script with FILTER:
=FILTER(A2:A,A2:A=MM1)
Or if you want it as an array for many rows
=BYROW (MM1:MM,LAMBDA(each,IF(each="","",FILTER(A2:A,A2:A=each))))
Check in this image how when I chose value "b" it returns the hyperlinked value in the coloured column
OPTION B with SCRIPT:
Please check if MM is column 351, if not change that number. Run it once from the console (it will give an error, but it's just for approving the permissions), then go to your sheet and start trying with your dropdown:
function onEdit(e) {
var ecolumn = e.range.getColumn()
if (ecolumn == 351){
var sheet = SpreadsheetApp.getActive()
var range = sheet.getActiveRange();
var value = range.getValue()
var lookuprange = sheet.getRange("A:A").getValues().map(n => n[0])
var index = lookuprange.indexOf(value)+1
if (index != 0){
var link = sheet.getRange("A"+index).getRichTextValue().getLinkUrl()
var richValue = SpreadsheetApp.newRichTextValue()
.setText(value)
.setLinkUrl(link)
.build();
range.setRichTextValue(richValue);}}
}
REFERENCE: Apps Script: how to get hyperlink from a cell where there is no formula
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 have a basic script to take numbers from a sheet and use them to create a range, as well as using the last column function. I have had the error range is too small for the posting range.
When I log the output for both the column and row numbers these come out as expected!
I thought initially, it was because one was a last column pull and the other was pulling an integer from the cell in the sheets, as they were coming with decimal places, so I have overcome this with the conversion to number and then removing the decimals with the .tofixed() but this does not work either. Any ideas?
function weeklyData() {
var sourcess = SpreadsheetApp.openById('1B93Oq2s9Nou5hVgOb3y3t15t9xnqRMBnrYkAed-oxrE'); // key of source spreadsheet
var sourceSheet = sourcess.getSheetByName('Measures & Answers'); // source sheet name - change to your actual sheet name
var lr = Number(sourceSheet.getRange(2,3).getDataRegion(SpreadsheetApp.Dimension.ROWS).getLastRow()).toFixed(0);
for(var i=0;i<lr+1;i++){
var dataValue = sourceSheet.getRange(i+2,3).getValue(); //This weeks numbers to update into table
var rowdataRange = sourceSheet.getRange(i+2,4).getValue(); //The row that the data needs to be pasted
var rowformat = Number(rowdataRange);
var row = rowformat.toFixed(0);
var pasteSheet = sourcess.getSheetByName('WHERE DATA ENDS UP'); // Data is to be pasted - change to your actual sheet name
var pasteColumn = pasteSheet.getRange(12,12).getDataRegion(SpreadsheetApp.Dimension.COLUMNS).getLastColumn()+1;
var column = pasteColumn.toFixed(0); // Column that is free for this weeks data
var pasteRange = pasteSheet.getRange(row,column,1,1);
Logger.log(pasteRange);
// pasteRange.setValue(dataValue);
}};
Your script works fine for me. I suspect this is an example script you've adapted from somewhere and trying to apply it to your data structure?
The reason you are getting the error is probably because the data in column 4 of your source sheet is not of number format? Either change your data or change the following line to the column containing numeric values.
var rowdataRange = sourceSheet.getRange(i+2,4).getValue();
This script is poorly written for this particular use.
You might want to check your Spreadsheet since it has lots of random values at random ranges.
When you use the following command Logger.log(pasteSheet.getLastColumn()); the number returned is 3753: which means that that is the next available column at which your data will be pasted.
The error message you were getting is due to the fact that the range was incorrect and you were passing wrong values in order to access it, which was most likely because of the values mentioned above.
Moreover, after cleaning all the unnecessary data, you can make use of the below script.
Snippet
function weeklyData() {
var spreadsheet = SpreadsheetApp.openById("ID_OF_THE_SPREADSHEET");
var sourceSheet = spreadsheet.getSheetByName("Measures & Answers");
var pasteSheet = spreadsheet.getSheetByName("WHERE DATA ENDS UP");
var valsToCopy = sourceSheet.getRange(2, 3, sourceSheet.getLastRow(), 1).getValues();
var rowsAt = sourceSheet.getRange(2, 4, sourceSheet.getLastRow(), 1).getValues();
var column = pasteSheet.getRange(12,12).getDataRegion(SpreadsheetApp.Dimension.COLUMNS).getLastColumn()+1;
for (var i = 0; i < valsToCopy.length; i++)
if (rowsAt[i][0] != "")
pasteSheet.getRange(parseInt(rowsAt[i][0]), parseInt(column)).setValue(valsToCopy[i][0].toString());
}
Explanation
The above script gathers all the data that needs to be copied as well as the rows associated with it. In order to make sure you don't end up using inappropriate values for the ranges, an if condition has been placed to make sure the value is not empty.
Reference
Sheet Class Apps Script - getLastColumn();
Sheet Class Apps Script - getRange(row, column, numRows, numColumns);
I also have sheet name ..."4"... as a number
But it shows error as "Exception: Range not found"
I think because my sheet name is a number, but I really need the sheet name as a number
I try this, it should work. but it turns out, it's not
var ss = SpreadsheetApp.getActiveSpreadsheet()
var shname = ss.getRange("Home!A1") // A1 contains value 4 as a number
ss.getRange("'" + shname + "'!A1").setValue("is it worked?") // how to fix this line?
for fully info
https://docs.google.com/spreadsheets/d/1GcyfutUP-vg23H9mpjfNLRQvTkOqp0c8dN-J4R8g9Hk/edit#gid=1502470412&range=A1
You need to get the value of the range/cell. Change the second line in the script to
var shname = ss.getRange("Home!A1").getDisplayValue() //getValue() should also work
and see if that works?
I have a custom function in my sheet which returns the color code for the font color of an inputted cell. It has been working fine for a few months, but today, one out of 61 cells using the custom function return an error saying "Internal error executing custom formula". I do not understand why this problem would exist only in this cell (it is the 6th of 61 cells with the function) nor how to fix it. The custom function follows here:
function FontColor(input) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange(input);
var testColor = cell.getFontColor();
return testColor
}
(Apologies if I formatted the code wrong)
This is a relatively simple code and there have been no problems for months. Today, I added 7 new rows where this custom function was being used in one cell per row, and the error appeared back up near the top in row 6. The formula is being used as such:
In cell Q3, the formula is =FontColor(W3), and in cell W3 is simply H3. H3 contains a dollar amount with a specific font color assigned. This was necessary for the custom formula as it required an input of H3 rather than H3... If that makes sense.
This is just a reference sheet I use to view payroll in my business, so I'm happy to reorganize it or modify any of these formulas/functions as necessary to make this work.
Thanks in advance for your assistance!
Thanks, the array fixed it. In case anyone runs into a similar problem, I updated the code as such:
function FontColor(input) {
if (input.map) {
return input.map(FontColor);
} else{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange(input);
var testColor = cell.getFontColor();
return testColor
}
}
Which I'm now using in batches of 100 down the column (I will need to use the function over 500 times, which is too much as well, but using =FontColor(W3:W100) works quickly and error free).
Thanks!