//create a copy for next week below in cell A31
function createWeek(){
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Sheet1!A4:AN30");
source.copyTo (ss.getRange("Sheet1!A31"));
var numRows = SpreadsheetApp.getActiveSpreadsheet().getLastRow() + 1;
}
I want to copy the range A4:A30 into the next empty row (in this case, row 31) BUT I'd like to do it every week so I need this row to be variable, because the next week the row will change. I could count the rows (numRows) but I don't know how to change the destination range so it can take into consideration the changing row (Sheet1!A???). THANKS!!
Use .getLastRow() + 1 .getLastRow() returns the last row containing data then + 1 to get the next row which will be empty.
Related
I have written this code with the purpose of cycling through a list of names. When the code finds the waitingName in the list it should replace the row that the name appears with a row of info. When I attempt to run this code it pastes the row of information at the top of the screen, not after row 17 where the list of names begins.
var waitingName = waitingSheet.getRange(6,13).getValue();
var edittedInfo = waitingSheet.getRange(6,3,1,12).getValues();
var waitingListNames = waitingSheet.getRange(17, 11, 105,1).getValues().flat();
var index = waitingListNames.indexOf(waitingName);
if (index > -1) {
waitingSheet.getRange(index + 1,1,1,12).setValues(edittedInfo);
}
Solution:
index takes values >=0, therefore index+1 takes values >=1. This
is why your code starts pasting from the first row.
If you want to paste the information after 17th row (including 17th row) then modify this:
waitingSheet.getRange(index + 1,1,1,12).setValues(edittedInfo);
to this:
waitingSheet.getRange(index + 17,1,edittedInfo.length,edittedInfo[0].length).setValues(edittedInfo);
since index can take values >=0, index + 17 takes values >=17 which is the desired starting row point.
Also, it is recommended to use .length instead, in case you want to change the size of edittedInfo down the road.
References:
getRange(row, column, numRows, numColumns)
length()
I'm trying to post an array into column P daily. Because it's posting daily, I'd like it to also grab the last row.
I'm pulling from another workbook but don't know how to get the paste to work.
Function () {
var toCopy = [copies data from spreadsheet 1]
/* trying to paste into Aggregate tab in spreadsheet 2*/
var ss2=SpreadsheetApp.openById(‘spreadsheet 2’);
var tsh2=ss2.getSheetByName('Aggregate');
var tsh2.getLastRow(???
}
How do I paste var toCopy into column P? The array is 15 columns wide.
You want to append the values to the next row of the last row of column "P" in the sheet of "Aggregate".
The values are 2 dimensional array which has 600 rows and 15 columns.
The number of rows is changed every run.
The number of columns is not changed.
After the values are put, you want to retrieve the last row number.
I could understand like above. If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Flow:
Retrieve the last row number of the column "P".
From your question, I'm not sure whether the last row of the column "P" is the last row of the sheet. So the last row is checked for only column "P".
Append "toCopy" to the next row of the last row of column "P".
Retrieve the current last row number.
Modified script:
Please set the values of toCopy and the Spreadsheet ID.
function myFunction() {
var toCopy = [copies data from spreadsheet 1]
var ss = SpreadsheetApp.openById('###');
var sheet = ss.getSheetByName('Aggregate');
// Retrieve the last row of the column "P".
var lastRowOfColP = 0;
var valuesOfColP = sheet.getRange("P1:P").getValues();
for (var i = valuesOfColP.length - 1; i >= 0; i--) {
if (valuesOfColP[i][0]) {
lastRowOfColP = i + 1;
break;
}
}
// Put "toCopy" to the next row of the last row of column "P".
sheet.getRange(lastRowOfColP + 1, 16, toCopy.length, toCopy[0].length).setValues(toCopy);
// Retrieve the current last row number.
var currentLastRow = lastRowOfColP + toCopy.length;
Logger.log(currentLastRow)
}
This modified script supposes that toCopy is 2 dimensional array.
References:
setValues(values)
getRange(a1Notation)
getRange(row, column, numRows, numColumns)
Looks like Set a range's values is what you're after.
function () {
var values = [
// Copied data from spreadsheet 1
]
// Paste into Aggregate tab in spreadsheet 2
var ss2 = SpreadsheetApp.openById(‘spreadsheet 2’);
var sheet2 = ss2.getSheetByName('Aggregate');
var range = sheet2.getRange('P1:P15');
range.setValues(values);
// Grab last row
var lastRow = sheet2.getLastRow();
}
PS: Watch out for the formatting in your code. It looks like you might not be using a proper text editor. Your formatting made function be Function and the quotations 'spreadsheet 2' become ‘spreadsheet 2’. I use VS Code and Vim, for example.
I have a range of cells (ex: L1:S22) which will send email alert when the values are > 0 in that particular range from active spreadsheet
So far its only sending from the active spreadsheet range. I want it to be called from all other workbooks which is stored in a folder and send email according to that
function CheckSales(){
var app = SpreadsheetApp;
var activeSheet = app.getActiveSpreadsheet().getActiveSheet();
var data=activeSheet.getDataRange().getValues();
var emailAddress=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("B2").getValue();
var resultArr=[];
//To Loop through the whole data Rows
for(var i=1;i<data.length;i++)
{
//Takes columns from L to S (To loop through the Columns)
for(var j=11;j<19;j++)
{
var cellVal=data[i][j];
Logger.log(cellVal)
if(cellVal>0)
{
//Stores the Part No, Month Header Value of the Column, Cell Value which is greater then 0
resultArr.push([data[i][0],data[0][j],cellVal])
}
}
}
if(resultArr.length>0)
{
var subject = 'Range exceeded Alert';
//Creates a body through the obtained values
var body='';
for(var m=0;m<resultArr.length;m++)
{
body+="For Part No "+resultArr[m][0].toString()+" and Month "+resultArr[m][1].toString()+", Value is "+resultArr[m][2].toString()+"<br>";
}
MailApp.sendEmail({to:emailAddress,subject:subject,htmlBody:body});
}
}
now the output showing the part number which have exceeded the values after 6 months.
For Part No 60009257001 and Month 7MO, Value is 800
For Part No 60009259007 and Month 12MO, Value is 28032
For Part No 60009260011 and Month >18MO, Value is 74670
For Part No 60009260012 and Month 12MO, Value is 17600
For Part No 60009260013 and Month 10MO, Value is 26389
I expect the output to show all the part numbers from different workbooks
for (var i in activeSheet) {
SpreadsheetApp.setActiveSheet(activeSheet[i])
var sheet = app.getActiveSheet();
var data = activeSheet[i].getDataRange().getValues();
}
you can loop through the different data ranges in the same way as done here with "data" Variable.
For example,
var data=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Battery').getDataRange().getValues();
//For Loop for data
data2=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Other Sheet").getDataRange().getValues();
//for Loop for data2
I want to select the range of 2 columns till the last row with a value. Now I use this code:
var test = declSheet.getRange('Potjes!A:B');
Form this sheet:
The result I get is this:
How can I select till the last row with value?
You can use Sheet.getDataRange function which:
Returns a Range corresponding to the dimensions in which data is
present.
It seems like you are only interested in column A and B so:
// var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// var sheet = spreadsheet.getActiveSheet();
var dataRange = sheet.getDataRange();
var desiredRange = sheet.getRange("A1:B" + dataRange.getLastRow());
// desiredRange will consist of A1:B5 in OP example
I have a table which works as expected, show all the data that falls between start and end dates(please look on comments for more info). only issue I am having right now is when I click next button on a table, the table header moves dates to next week dates and shows the data for that week but also it shows the data from last week as well. what I needed was if it goes to next week date it should show data only for that week (no rows/data from last week).
//this is only an eg:
function test() {
//this is the table header start date(in pic)6/4/17
var test1 = document.getElementById("testing").innerHTML;
//this is the table header close date(in pic)6/10/17
var test2 = document.getElementById("testin").innerHTML;
//this is the function if dates is between start and end its displays the data in a tabel
function dateInRange(dbdates, start, end) {
if (end >= dbdates && start <= dbdates) {
return true;
} else {
return false;
}
}
var columns = ['orange']
var table = document.getElementById('tablet');
for (var i = 0; i < (testvalues.length - 1); i = i + 5) { //looping all values from db
if (dateInRange(testvalues[i + 2], test1, test2)) {
var row = table.insertRow(-1);
var cell1 = row.insertCell(-1);
var cell2 = row.insertCell(-1);
//more logic
//this is just an ex:
//this is next button on table
$("next").click(
test();
}
You need something like the following at the start of your dateInRange function:
function dateInRange(dbdates, start, end) {
dbdates=Date(dbdates);
start=Date(start);
end=Date(end);
//rest of function
}
This will allow you to do the Date comparison you are attempting. You could convert the values to Dates before passing them to the function but this keeps your code cleaner.
In regards to removing/replacing all the existing rows except for the header, a good solution is here: Delete all rows in an HTML table
In order to compare dates, you need to tell Javascript your string is a date and not just a regular string.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
You should remove the data of table before for loop , try it:
$("#tablet tbody tr").remove();