I'm continually adding new rows to Google Sheets with numerical values in Column C. I need a script (not a formula!) to do 2 things with those numbers.
Change the number format to remove commas from any numbers
Add 200 to each number
I've got the script for part 1...
function setFormat(){SpreadsheetApp.getActiveSheet().getRange("C2:C").setNumberFormat('##########0');}
But I need help with part 2.
Both scripts need to occur immediately upon creation of the number in Column C.
I can't use formulas for these changes. I need to use scripts.
Thank you!
You can write whole code within onEdit() function
like this:
function onEdit(e){
var range = e.range;
if(range.getColumn() == 3){ //Column C is 3rd column
SpreadsheetApp.getActiveSheet().getRange("C2:C").setNumberFormat('##########0');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var r = e.source.getActiveRange();
var cell = r.getA1Notation();
var tempval = sheet.getRange(cell).getValue();
var numval = parseFloat(tempval)+200;
sheet.getRange(cell).setValue(numval);
}
}
Hope this will help you.
Thanks.
Related
I've got a script that's doing some onEdit formatting on a sheet of mine. All the rest is working well, and I wanted to include a line that deletes spaces from number values I'm importing. That last line is not working.
Any ideas what I'm missing here?
function onEdit(e) {
var cell = e.range;
var sh = e.source.getActiveSheet();
if(sh.getName() === "Trading Journal") {
cell.setBackground('#fff');
cell.setFontSize(10)
cell.setFontFamily()
cell.replace(/\s/g, "")
}
}
https://docs.google.com/spreadsheets/d/1rkjO-ITeLdIHq-LLHfHcp6-1j1R0-giS6HGbwYdJ5Ek/edit?usp=sharing
Did a bit of research regarding this, and it seems that .replace() is a string method and therefore may not work with numbers. Reference
But if you only need to remove whitespaces from numbers, here is a simple solution:
function rSpaces() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell = ss.getActiveCell();
cell.trimWhitespace();
}
You can assign this on an onEdit trigger and check this documentation for any font modifications you want to add.
My script is supposed to move a row from one google spreadsheet to another based on a value in a cell. It fails to do so, and I think it's because of the onEdit() function but can't figure it out. I found a solution for different sheets in the same spreadsheet but not in different spreadsheets. My code is below.
function onEdit(e) {
var sss = SpreadsheetApp.getActiveSpreadsheet();
var ss = e.source.getActiveSheet();
var r = e.source.getActiveRange();
if(s.getName() == "source_sheet" && r.getColumn() == 7 && r.getValue() == "Pavlovo"){
//Get full range of data
var SRange = ss.getRange(1, 3, 1, 5);
//get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
//get the data values in range
var SData = SRange.getValues();
// tss = target spreadsheet
var tss = SpreadsheetApp.openById('1_t7BDCWgHJDip_cndzVashxLDQ_pVS6obi3I9TB_EJI');
var ts = tss.getSheetByName('Vitosha'); // ts = target sheet
//set the target range to the values of the source data
ts.getRange(A1Range).setValues(SData);
}
}
You are using a simple trigger onEdit. Such triggers run without authorization and therefore are subject to several restrictions, such as:
They can modify the file they are bound to, but cannot access other files because that would require authorization.
This is why SpreadsheetApp.openById fails to execute in your code.
Solution: rename the function to something other than onEdit, and add an installable trigger running on edits. An installable trigger runs as you (i.e., the user who created it), and therefore can access any files that you can access.
Sorry if this is a bit newbie.
What I'm trying to do is take a column and delete the values in some of its cells if they do not contain a formula.
function clean() {
var ss = SpreadsheetApp.openById("KEY_REMOVED_FOR_OBVIOUS_REASONS");
var sheet = ss.getSheetByName("TEST");
var limit = sheet.getMaxRows();
for(var i = 6; i < limit; ++i){ // Declares variable "i", which represents the row we are currently checking. Basically says "if our row isn't the last one, execute this block then add to i".
var range = sheet.getRange(i,2);
if (range.getValue() != "") { // Leverages the fact that .getValue() cannot return functions. If our range is blank, execute this block.
range.clear();
}
}
}
I established a loop which checks every row one by one. What I'm wondering is, is there a more efficient way to do this than having to call the server every time I do the loop?
getValues() exists of course, but I'm not sure how one might interact with only the desired cells returned by that.
I did some testing. .getValues() will return the computed values if there are formulas.
The following code works for me.
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getLastRow();
var range = sheet.getRange(1,1, rows);
var values = range.getFormulas();
range.clear();
range.setFormulas(values);
}
Short answer
Regarding the too many calls issue, use getLastRow() instead of getMaxRows() and use the returned value (number of rows) to get the required range in just one call.
Explanation
getLastRow() returns the position of the last row that has content while getMaxRows() returns the maximum height of a sheet regardless of content.
getRange(row, column, numRows) returns in just one call the complete range.
friends! In my sheet I have a column with duration format.
http://dl1.joxi.net/drive/0007/2131/485459/150902/fc427ebb50.jpg
I'm trying to get values of this column with this function:
function getSheetValues(startRow, startCmn, maxColumn) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mainSheet = ss.getSheets()[0];
var range = mainSheet.getRange(startRow, startCmn, mainSheet.getMaxRows(), maxColumn);
return range.getValues();
}
var mainSheetData = getSheetByNameValues(2, 1, 9);
var test = mainSheetData[2][6];
Logger.log(test);
As the result I get wrong values. Example: for third row (00:23:00.000) I get this: Sat Dec 30 02:13:47 GMT+05:53 1899. Probably due to date auto formatting. How can I avoid this? is there any ways to get value (00:23:00.000) as planar text, without changing format of the column itself?
Hope for your help, friends.
Assuming you have full control over the spreadsheet...If you truly just want the string representation of '00:20:00.000' the easiest thing to do is to set the format of that column in the actual spreadsheet itself, to be plain text, i.e.
Select the column
Format -> Number -> Plain Text
Then it won't be converted to a date and you should get the raw string value you're after.
If you can't control (or otherwise guarantee) what the format of that column is, you're going to have to end up doing something like what jeremy has suggested in his comment.
You could also do it all programatically, copying the range to another cell, set numberFormat to text, then delete this copied range (or leave it):
function go(){
var ss = SpreadsheetApp.getActiveSheet(), rang1 = ss.getRange('A11'), rang2 = ss.getRange('A12');
rang1.copyTo(rang2);
Logger.log(rang2.setNumberFormat('#STRING#').getValue());
rang2.clear();
};
This of course can be done to entire ranges at once.
If you have a duration in your cell (B2 = 02:00 for example), and you want to get the same value as a result (02:00), you can use this script :
var s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('name_of_sheet');
var l = SpreadsheetApp.getActiveSpreadsheet();
var duration = s.getRange("value_duration").getValue();
var d = Utilities.formatDate(duration, l.getSpreadsheetTimeZone(), "HH:mm");
Logger.log(d);
I gave a name to "B2" : value_duration
I have this code that I found online and it works as needed, but it works for all my sheets or the tabs at the bottom if you want to call them. I want it to work
function onEdit(event)
{
var sheet = event.source.getActiveSheet();
var eventRange = event.source.getActiveRange();
var eventColumn = eventRange.getLastColumn();
if (eventColumn == 1)
{
var stampRange = eventRange.offset(0,10);
stampRange.setValue(new Date());
}
}
This is the original code, I tried adding in line 4 the following but i can't get it to work. I'm not experienced with javascript but I need your help as i'm trying my best. Thank you.
if(sheet.match(/*.13/)){
This is the line I added. Based on my reading online, the script should works only if the sheet name ends with 13. But it's not working.
You've got the right idea, but sheet is a Sheet Object, while .match() is a String method. Use the Sheet.getSheetName() method to get the name of the sheet (the words on the tab).
In an onEdit(), you usually want to bail out without investing much processing time, so you should put the test for the sheet name as early as possible.
If you want to match "Sheet13" exactly, you should test for just that - because your regex will also match "Apollo13" and "a13a", for example.
function onEdit(event)
{
var sheetName = event.source.getActiveSheet().getSheetName();
if (sheetName.match(/.13/) == null)
// These aren't the droids you're looking for...
return;
var eventRange = event.source.getActiveRange();
var eventColumn = eventRange.getLastColumn();
if (eventColumn == 1)
{
var stampRange = eventRange.offset(0,10);
stampRange.setValue(new Date());
}
}