I am very new to using macro's in google sheets, and struggling to replicate easy VBA codes
I am trying to find the last row of column A, then add formulas to various columns and copy down to the last row.
In VBA it is as simple as **("B2:B" & Lastrow).select
However, this is not so straight forward.
I have found and manipulated the following, however it always goes to the very last row rather than the last row of data
var sheetName = "Week 1";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
spreadsheet.getRange('E7').activate()
.setFormula('=iferror(if(match($B7,Names,0),VLOOKUP($B7,Special,3,false),0),0)');
var lr = sheet.getLastRow();
var fillDownRange = sheet.getRange(7, 5, lr-1);
sheet.getRange('e7').copyTo(fillDownRange);
What needs to be done to the above so that it only fills down to the last row of data in column A
All and any help greatly appreciated
EDIT-
I have update the code, (Obviously wrongly) and added to the s/sheet under tools, copy on tab
var sheetName = "Week 1";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
var lastRow = sheet.getLastRow();
var range = sheet.getRange("A7" + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
var lr = getLastDataRow(sheet,"A")
//Saturday Formula
spreadsheet.getRange('E7').activate()
.setFormula('=iferror(if(match($B7,Names,0),VLOOKUP($B7,Special,3,false),0),0)');
var fillDownRange = sheet.getRange(7, 5, lr-1);
sheet.getRange('E7').copyTo(fillDownRange);
as between these two rows is a light bulb sayingabout unreachable code, and everything after and including Var = lr** is greyed out
}
var lr = getLastDataRow(sheet,"A")
Correction of your first code
function myFunction() {
var sheetName = "Week 1";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
sheet.getRange('E7').setFormula('=iferror(if(match($B7,Names,0),VLOOKUP($B7,Special,3,false),0),0)');
var lr= sheet.getRange('A7').getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow()
var fillDownRange = sheet.getRange(7, 5, lr-6,1);
sheet.getRange('E7').copyTo(fillDownRange);
}
Instead of var lr = sheet.getLastRow(); , try
lr = sheet.getRange('A1').getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow()
change A by E if needed.
edit : Change A1 by A7 si A4 and A6 are empty.
If you have discontinous value, prefer
function getLastDataRow(sheet,col) {
// col in letter
var lastRow = sheet.getLastRow();
var range = sheet.getRange(col + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
}
and
lr = getLastDataRow(sheet,"A")
Related
I'm trying to get this code to work but keep getting a "TypeError: offset.copyTo is not a function". The script is supposed to take one value from sheet "Budget" and copy it into the next available row in column F on sheet "Projected".
I have tried playing around with syntax and other ways to copy but to now avail, any help would be greatly appreciated
function OffsetRecord() {
var sheet = SpreadsheetApp.openById("xx").getSheetByName("Budget");
var offset = sheet.getRange('I5').getValue();
var destSheet = SpreadsheetApp.openById("xx").getSheetByName("Projected");
var colF = destSheet.getRange('F:F').getValues();
var fcolF = colF.filter(function (e) {return e[0];});
var firstEmptyRowIncolF = fcolF.indexOf('')+1;
offset.copyTo(firstEmptyRowIncolF, {contentsOnly: true});
copyTo expects ranges as parameters, not values or column numbers, see here
Modify your code as following:
function OffsetRecord() {
var spreadsheet = SpreadsheetApp.openById("XX");
var sheet = spreadsheet.getSheetByName("Budget");
var offset = sheet.getRange('I5');//without ".getValue();"!
var destSheet = spreadsheet.getSheetByName("Projected");
...
firstEmptyRowIncolF = ...;
var destinationColumn = 1;
var range = destSheet.getRange(firstEmptyRowIncolF, destinationColumn);
offset.copyTo(range, {contentsOnly: true});
}
I have this script here, but I want it to only run on ONE specific sheet named "Nes Smart Data" (SheetNo5). Currently it runs on all sheets and puts the data on cells where I don't want them to be. Can you help me correct the code? Thanks a lot!
function onEdit(e){
var sheet = SpreadsheetApp.getActiveSheet();
var range = e.range;
var column = range.getColumn();
var row = range.getRow();
var text;
if (column==11) { //Replace 2 with the column number of your comments cell//
var newRange = sheet.getRange(row,column-5); //If the date is in the next column
var today = Utilities.formatDate(new Date(),Session.getScriptTimeZone(),'dd.MM.yyyy');
newRange.setValue(today);
}
if (column == 11){
text = sheet.getRange(row, 23).getValue();
sheet.getRange(row, 23).setValue(text + e.value+".");
}
}
Replace the following variable and things should work as desired -
Current:
var sheet = SpreadsheetApp.getActiveSheet();
Modification proposed:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Nes Smart Data');
Let me know if it doesn't!
Certain cell in rows have hyperlink. need hyperlink to follow row when copied from sheet to sheet based on other cell value
function moveToTab() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var range = ss.getActiveRange();
var col = range.getColumn();
Logger.log(col);
var aSheet = ss.getActiveSheet();
var header = aSheet.getRange(10,col,1,1).getValue();
Logger.log(header);
if (header.toLowerCase() !== 'issued to') return;
var target = range.getValue();
if (target.toString().trim() === '') return;
if (target.toString().toLowerCase() === aSheet.getName().toLowerCase()) return;
var sheets = ss.getSheets();
var tSheet = sheets.filter(function(sheet) {
if (sheet.getName().toLowerCase() == target.toLowerCase()) return true;
else return false;
})[0];
if (!tSheet) return;
var row = range.getRow();
var values = aSheet.getRange(row, 1, 1, aSheet.getLastColumn()).getValues();
tSheet.appendRow(values[0]);
aSheet.deleteRow(row);
I know I am missing something simple. In my work book column "F" has a hyperlink to files. I am using the above script to move certain rows from sheet to sheet based on value in Column "K" and would like the hyper link in the cell in column "F" to follow teh row from sheet to sheet. What am i missing
Please see the previous post on this question;
Auto Sort not working on Multiple Sheets within one Google Sheet
Here's the good script from that last session;
function onEdit(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = event.source.getActiveSheet().getName()
var editedCell = event.range.getSheet().getActiveCell();
if(sheet=="Loan Inquiries"){
var columnToSortBy = 2;
var tableRange = "A3:G99"; //range to be sorted
if(editedCell.getColumn() == columnToSortBy){
var range = ss.getActiveSheet().getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
}}
else if(sheet=="Deals in Escrow"){
var columnToSortBy = 7;
var tableRange = "A3:I99"; //range to be sorted
if(editedCell.getColumn() == columnToSortBy){
var tableRange = "A3:I99"; //range to be sorted
var range = ss.getActiveSheet().getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
}
else{return}
}}
function test_onEdit() {
onEdit({
user : Session.getActiveUser().getEmail(),
source : SpreadsheetApp.getActiveSpreadsheet(),
range : SpreadsheetApp.getActiveSpreadsheet().getActiveCell(),
value : SpreadsheetApp.getActiveSpreadsheet().getActiveCell().getValue(),
authMode : "LIMITED"
});
}
This thread successfully resolved my question, however, now what I want to do is to Auto Sort a 3rd Sheet, which is locked to others, but not to myself. The 3rd Sheet will be titled "Karlan Production for 2017". I also want this 3rd sheet to pull all new data from the 2nd Sheet "Deals in Escrow" and in the same formatting as the 2nd Sheet and to auto sort as the data is imported to the 3rd Sheet, but also be able for me to input new deals manually as well in the 3rd Sheet and still be able to auto update.
I believe this will do what you want:
function onEdit(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = event.source.getActiveSheet().getName()
var editedCell = event.range.getSheet().getActiveCell();
if(sheet=="Loan Inquiries"){
var columnToSortBy = 2;
var tableRange = "A3:G99"; //range to be sorted
if(editedCell.getColumn() == columnToSortBy){
var range = ss.getActiveSheet().getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
}}
else if(sheet=="Deals in Escrow"){
var columnToSortBy = 7;
var tableRange = "A3:I99"; //range to be sorted
if(editedCell.getColumn() == columnToSortBy){
var range = ss.getActiveSheet().getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
combineData()
}}
else if(sheet=="Karlan Production for 2017"){
var columnToSortBy = 7;
if(editedCell.getColumn() == columnToSortBy){
combineData()
}}
else{return}
}
function combineData(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s=ss.getSheets()[1]
var lr = s.getLastRow()
var s1=ss.getSheets()[2]
var lr1 = s1.getLastRow()
var lc1=s1.getLastColumn() //get the last column of 'Karlan Production for 2017'
var rng=s.getRange(3, 1, lr,lc1).getValues() //get 'Deals in Escrow' values
var rng1=s1.getRange(3, 1, lr1,lc1).getValues() //get 'Karlan Production for 2017' values
var rng2=[]
var rng2=rng.concat(rng1) //combine 'Deals in Escrow' and 'Karlan Production for 2017' values (all rows)
removeDuplicates(rng2) // call remove duplicates sending combined array
}
function removeDuplicates(data) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var s1=ss.getSheets()[2]
var newData = new Array();
for(i in data){
var row = data[i];
var duplicate = false;
for(j in newData){
if(row.join() == newData[j].join()){
duplicate = true;
}
}
if(!duplicate){
newData.push(row);//create array of non duplicate rows
}
}
s1.clearContents(); //clear 'Karlan Production for 2017'
var sor=s1.getRange(3, 1, newData.length, newData[0].length).setValues(newData); //set new array
sor.sort( { column : 7, ascending: true } );//sort new data
}
After adding the new sheet, click the arrow on the sheet tab.
Click Protect Sheet. In the popup, enter a description and click
the Sheet button. Click the Set permissions button. In the Range
editing permissions popup click the arrow by 'Only you'. Select
'Custom' and add the email address of the other person you want
to be able to edit the sheet. Click done.
I'm trying to create a script in google sheets. What I'm trying to do is to search the value of a cell (let's say A2 sheet1), then change the value of another cell (B2 sheet2). When the first iteration is over, proceed to the next row (A3 sheet1) and again change the value of B2. Also wait 10 secs at the end of every iteration.
I don't know why it is not working. After I run the code it just says "Finished", but nothing has changed.
function test() {
var ss = SpreadsheetApp.getActive();
var gensheet = ss.getSheetByName('Generador SE');
var clientsheet = ss.getSheetByName('Clientes SE');
var clientrange = clientsheet.getRange("A1:C200");
var clientsquantity = clientrange.getNumRows()
var servicetochange = gensheet.getRange(2,2)
var i;
for (i=1;i>clientsquantity;i++){
var currentservice = clientrange.getRange(i,1).getValue;
servicetochange=gensheet.getRange().setValue(currentservice);
Utilities.sleep(10000);
}
}
I have changed some code in the loop ... now it does something (but I can't be sure it does what you expect):
function test() {
var ss = SpreadsheetApp.getActive();
var gensheet = ss.getSheetByName('Generador SE');
var clientsheet = ss.getSheetByName('Clientes SE');
var clientrange = clientsheet.getRange("A1:C200");
var clientsquantity = clientrange.getNumRows()
var servicetochange = gensheet.getRange(2,2)
var i;
for (i=1;i<clientsquantity;i++){
var currentservice = clientrange.offset(i,0,1,1).getValue();
servicetochange.offset(i,0,1,1).setValue(currentservice);
Utilities.sleep(10000);
}
}