Extract ranges from multiple workbooks / sheets - javascript

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

Related

Paste data starting in column P of google sheets (via script)?

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.

Google Sheets Calendar

I would like some help in pulling certain data from a google form linked to a spreadsheet and set a certain value in the date of a certain employee. For example if he/she marked as Vacation Leave then the letter V will be marked under the Date. I have attached the link to my calendar to give an example, I have tried searching the net and this is my second time posting here.
I started the Code but I am stuck in actually finding the Cell for a certain date and inserting the letter.
https://docs.google.com/spreadsheets/d/1uFTR2_B7T0QBr7fTflByFffmmiszbNj_RaCvLEfYRCA/edit?usp=sharing
function setData(){
//Spreadsheets
var ss = SpreadsheetApp;
var data = ss.getActiveSpreadsheet().getSheetByName("Data");
var calendar = ss.getActiveSpreadsheet().getSheetByName("Calendar");
//Get last row / Individual Cells from Form
var lRow = data.getLastRow();
var lRowData = data.getRange(lRow,1,1,17).getValues();
var approval = data.getRange(lRow,17,1,1).getValue();
var leave = data.getRange(lRow,9,1,1).getValue();
var agentName = data.getRange(lRow, 5,1,1).getValue();
var dateBefore = data.getRange(lRow, 10,1,1).getValue();
var dateAfter = data.getRange(lRow, 11,1,1).getValue();
//Calander Variable Arrays
var allDates = calendar.getRange("LA1:NJ1").getValues();
var allNames = calendar.getRange("A4:A160").getValues();
for(var i = 0; i<allNames.length; i++){
if (approval === "Approved" && allNames[i][0] === agentName){
//Here I need it to insert the dates under the correct name and date with a value of V H S M U T.
};
};
};
You are building a spreadsheet-based Leave Calendar based on information from a form response. Based on your existing Calendar, you are having problems identifying the relevant leave dates, and then filling calendar cells to indicate proposed leave.
The problem is there are no fields in rows 1,2 or 3 of Calendar that have the same date format as the Start Date and End Date fields on Form. As a result, there's no easy way to search for a match of the form data fields.
The solution, in my view, is to change the format of the date fields in rows 2 and 3 and enable a search to be match.
Row 2
the existing format is "d" - day of the month (Numeric)
change the format to match the Form Start/End dates: "d-MMM-yyyy".
the font colour for this field can be used to "hide" these dates, and the column width reduced also.
Row 3
the existing format is "E" - day of the week (Name)
change the format to combine the existing formats of rows #2 and #3 - "E-d"
Aspects of the script
the form data is retrieved as getDisplayValues(). The purpose of this is to get the date as a string to facilitate the search.
Two sets of Calendar data are obtained
1) the dates row (Row#2)
2) the names column (Col #1). The Javascript map method is used to convert names from a 2D array to a 1D array. This creates an array that can be easily searched.
the Javascript indexOf method is used to find the Column match for the start and end dates, and to match the name in the Calendar Column (which yields the Row)
the script loops through the number of days leave to create a temporary array of "V" values.
using the row number and the start and end column numbers, a range can be defined on calendar and then updated from the values in the temporary array.
Presumably your function would be triggered by onFormSubmit(e).
Form data
Calendar - Before
Calendar - After
function so5871726503(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var form = ss.getSheetByName("Form");
var calendar = ss.getSheetByName("Calander");
//get form data
var formdataRange = form.getRange(6,1,1,9);// test data
var formData = formdataRange.getDisplayValues(); // display values to format date as string
//get the employee name, start date and end date
var formName = formData[0][1];
var formSD = formData[0][3];
var formED = formData[0][4];
// Logger.log("DEBUG: name = "+formName+", start date = "+formSD+", end date = "+formED);
//get Calendar variables
var calLR = calendar.getLastRow();
var calLC = calendar.getLastColumn();
var caldateStart = 9;
var calnameStart=4;
// get Calendar dates
var calDateRange = calendar.getRange(2,caldateStart,1,calLC-caldateStart+1);
var calDateValues = calDateRange.getDisplayValues();
// get Calendar names
var calNameRange = calendar.getRange(calnameStart,1,calLR-calnameStart+1);
var calNameValues = calNameRange.getValues();
var calNames = calNameValues.map(function(e){return e[0];});//[[e],[e],[e]]=>[e,e,e]
// there should be some error checking on indexof results = -1 in case there is a mismatch.
// find form start date in calender
var startdateresult = calDateValues[0].indexOf(formSD);
var startdateresultcol = startdateresult+caldateStart;
// Logger.log("DEBUG: start date result = "+startdateresult+", column = "+startdateresultcol);
// find form end date in calender
var enddateresult = calDateValues[0].indexOf(formED);
var enddateresultcol = enddateresult+caldateStart;
// Logger.log("DEBUG: end date result = "+enddateresult+", column = "+enddateresultcol);
// find form name in calender
var nameresult = calNames.indexOf(formName);
var nameresultrow = nameresult+calnameStart;
// Logger.log("DEBUG: name result = "+nameresult+", row = "+nameresultrow)
// calculate number of days leave
var daysleave = enddateresultcol-startdateresultcol+1
// Logger.log("DEBUG: days leave = "+daysleave)
// create array variable to hold leave data
var leave=[];
// loop to create data to fill Calendar
for (i=0;i<daysleave;i++){
leave.push("V");
}
// Logger.log(leave); // DEBUG
// build leave range
var calLeave = calendar.getRange(nameresultrow,startdateresultcol,1,daysleave);
//Logger.log(calLeave.getA1Notation()); //DEBUG
// Update the leave range
calLeave.setValues([leave]);
}

Array size issue when copying GoogleSheet to another sheet

I need to get a sheet of data as an array, perform some processing, then save it to another sheet.
Without attempting any intermediate processing,
var booking = getRowsData(sheet); //should create the array
var range = target.getRange(lastDBRow+1,1,numRows); //obtains the destination range (numRows logged as 3)
then
range.setValues([booking]); //tries to save the values but throws an error "Incorrect range height, was 1 but should be 3"
Checking the booking object with
var response = ui.prompt(booking.length + " " + booking[0].length, ui.ButtonSet.OK);
shows length 3 and
booking[0].length (which I understood to be the columns) as undefined....
So why the row length mismatch?
And should the columns value be undefined?
My thanks in advance!
The full code is :
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('New booking');
var headers = sheet.getRange(1, 1,1,sheet.getLastColumn());
var lastRow = sheet.getLastRow();
var numRows= lastRow-1
var target = ss.getSheetByName('Test'); //temp redirect to test sheet
var lastDBRow =target.getLastRow(); //we want to append the data after this
var booking = getRowsData(sheet); // Get data to copy as array object
= ui.prompt(booking.length + " " + booking[0].length, ui.ButtonSet.OK);
// Returns 3 Undefined
var response = ui.prompt(numRows, ui.ButtonSet.OK);
// Returns 3
var range = target.getRange(lastDBRow+1,1,numRows); // Must match range size
range.setValues([booking]);
}
Looks to me like there is no column count defined in this range:
var range = target.getRange(lastDBRow+1,1,numRows); // Must match range size
range.setValues([booking]);
Without being about to use debug, I can't confirm my idea, but I see you are defining the starting row, the starting column and the number of rows, but not the number of columns. I don't know for sure if that error message would be caused by that, but it looks like something to consider.

Get range of a sheet up to the last cell containing data

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

Combine a Date & a Time Column in Google Sheets for a createEvent() function [duplicate]

This question already has an answer here:
How to merge date and time as a datetime in Google Apps Spreadsheet script?
(1 answer)
Closed 7 years ago.
I need to combine two columns into one dateTime column that can be reading by a createEvent function for startDate.
Column F is the date (mm-dd-yyyy) & Column G is the time (HH:mm PM/AM). I am currently combine them in Column H with the following code:
function conCat() {
var sheet = SpreadsheetApp.getActiveSheet();
var numberOfRows = sheet.getLastRow().toString();
var range = sheet.getRange(2,1,numberOfRows,12);
var values = range.getValues();
var consultedAlreadyFlag = sheet.getRange(2,10,numberOfRows,12);
var sheetName = sheet.getSheetName();
//show updating message
consultedAlreadyFlag.setFontColor('red');
var numValues = 0;
for (var row = 2; row < values.length.toString(); row++) {
//check to see if name and type are filled out - date is left off because length is "undefined"
if (values[row][3].length > 0) {
currentStatus = values[row][1];
//check if it's been entered before
if (values[row][9] != 'EMAIL_SENT'){
sheet.getRange(row+2,8,1,1).setValue('=F' +(row+2)+ '+G' +(row+2));
}
else{
//sheet.getRange(row+2,10,1,1).setValue('EMAIL_SENT');
}
numValues++;
}
//hide updating message
consultedAlreadyFlag.setFontColor('green');
}
}
This code isn't working because when someone submits the form, and the code combines the columns, I cannot get the format to come out as "mm-dd-yyyy HH:mm:ss" which I feel I need in order for my createEvent function to work.
How can I get it to combine the two columns to get the format I need?
I wouldn't bother trying to combine the two columns. There's no point in adding another column, I don't think. You can use JavaScript methods like setHours():
function fncAddToCalender() {
var theEventDate = //To Do - Get the Date
var startMin = value from sheet cell
theEventDate.setHours(15); //Sets event date to 3pm
theEventDate.setMinutes(startMin);
var cal = CalendarApp.getDefaultCalendar();
var event = cal.createEvent(calndrTitle, theEventDate, endDate, {
description : theDescrptn,
location : theLocation,
guests : guestToInvite,
sendInvites : true
});
};
The format in the spreadsheet is probably set to "date". And getting the values with Apps Script will probably return a value that's already in a date format. If you have the columns formatted as text, you'd need to change the values to a date.

Categories