I have an issue that I cannot figure out. I wrote some code to take an item from a sheet called "Bag Label" and then look up its location in another sheet called "Inventory & Lots". and then to copy some of the cells to the right of that location.
It works well as long as the location is not row 80 or higher in sheet "Inventory & Lots". For the life of me, I cannot figure out why row 1 to 79 works but not 80+.
var ss=SpreadsheetApp.getActiveSpreadsheet();
var menuSheet=ss.getSheetByName("Bag Label");
var inventorySheet = ss.getSheetByName("Registered Lots");
var inventorySheetData = ss.getSheetByName("RegisteredLots - NO DEL");
var nextRow = inventorySheetData.getLastRow()+1;
var inventoryLots = ss.getSheetByName("Inventory & Lots");
var inventoryCalc = ss.getSheetByName("Inventory Calculation");
var inventoryCalcData = inventoryCalc.getRange(2,1,inventoryCalc.getLastRow()-1,4).getValues()
var inventoryData = inventoryLots.getRange(1,1,inventoryLots.getLastRow()-1,10).getValues()
var columnIndex = 0
var matchText = menuSheet.getRange("D2").getValue()
var kgNeed = menuSheet.getRange("AB2").getValue()
var index = inventoryData.findIndex(row => row[columnIndex] === matchText)
var rowNumber = index +1
var kg1 = inventoryLots.getRange(rowNumber,2).getValue();
var lot1 = inventoryLots.getRange(rowNumber,3).getValue();
var bb1 = inventoryLots.getRange(rowNumber,4).getValue();
var kg2 = inventoryLots.getRange(rowNumber,5).getValue();
var lot2 = inventoryLots.getRange(rowNumber,6).getValue();
var bb2 = inventoryLots.getRange(rowNumber,7).getValue();
var kg3 = inventoryLots.getRange(rowNumber,8).getValue();
var lot3 = inventoryLots.getRange(rowNumber,9).getValue();
var bb3 = inventoryLots.getRange(rowNumber,10).getValue();
Use array methods such as Array.filter() to get data more easily. Try this pattern to get started:
const ss = SpreadsheetApp.getActive();
const matchText = ss.getRange('Bag Label!D2').getValue();
const inventoryLots = ss.getSheetByName('Inventory & Lots');
const matchingData = inventoryLots.getDataRange().getValues()
.filter(row => row[0] === matchText);
Related
I don't need empty how I should do, I think problem in lastrow funtion (see in picture)
I want to create pdf with can select area and select sheet without hide other sheet is posible ?
this is code(copy from some youtube)
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var shtprint = ss.getSheetByName("Test print");
var shtkishu = ss.getSheetByName('proplan');
var shmaster = ss.getSheetByName('Master')
var shsetting = ss.getSheetByName('SETTING')
var shdata = ss.getSheetByName('DATA')
var shzttpp = ss.getSheetByName('ZTTPP038')
var LastRow = shtkishu.getRange(5,8).getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
var kishu = shtkishu.getRange(5,8,+LastRow).getValues().flat().map(v=>[v.slice(-7)]);
Logger.log(kishu)
for (var i = 0;i < kishu.length;i++){
shtprint.getRange('E2').setValue(kishu[i][0]);
var name = shtprint.getRange('E2').getValue();
shtkishu.hideSheet();
shmaster.hideSheet();
shsetting.hideSheet();
shdata.hideSheet();
shzttpp.hideSheet();
var folder = DriveApp.getFoldersByName("test").next();
var Blob = shtkishu.getParent().getBlob().getAs('application/pdf');
folder.createFile(Blob).setName(name);
}
shtkishu.showSheet();
}
don't know how to fix that
I have one sheet for a group of people. I'm trying to make a script so that whenever they edit the sheet, the time that they last edited will show up in a sheet in another document.
A sample row from the group sheet looks like this:
col1|...|col10|person name|...|col17
and the sheet that the time should be going to looks like this:
person name|col2|...|col10|date last edited.
this is the code that I've been trying and it is not working so far:
function onEdit(e) {
var row = e.range.getRow();
var col = e.range.getColumn();
var time = new Date().toLocaleString("en-US", {timeZone: "America/Toronto"})
var ID = "sheet ID"; //The ID for the second sheet goes here
var sheet = SpreadsheetApp.OpenById(ID);
var target = sheet.getSheetByName("Sheet2");
var lastColumn = target.getLastColumn();
var lastRow = target.getLastRow();
var range = target.getRange(1,1,lastRow, lastColumn);
var data = range.getValues();
var userEmail = e.user.getEmail();
var array = userEmail.split(".");
var firstName = array[0];
var secondSheet = e.source.getActiveSheet();
var lastRowSecond = secondSheet.getLastRow();
var lastColumnSecond = secondSheet.getLastColumn();
var secondRange = secondSheet.getRange(1,1,lastRowSecond, lastColumnSecond);
var secondData = secondRange.getValues();
var nameRow = secondData[row-1];
var nameColumn = nameRow[10];
var secondPersonarray = nameColumn.split(" ");
var secondPersonFirstName = secondPersonarray[0];
for (var i=1; i<lastRow; i++) {
var sheetRow = data[i];
var name = sheetRow[0].split(" ");
var firstNameSheet = name[0];
if ((firstName == firstNameSheet) && (firstName == secondPersonFirstName)) {
target.getRange(sheetRow, 10).setValue(time);
}
}
}
I've done something similar, only with one document, so I know the problem isn't with the time.
I'm trying to:
Find the Row number the Value from Cell 'PENALTY LINEUP ENTRY'!B10 on the sheet called Penalties
Copy the Data for that person 'PENALTY LINEUP ENTRY'!C10:J11 to the adjacent cells on the Penalties sheet.
The problem is that the data gets written to the wrong row and I don't know why.
Here is my spreadsheet:
https://docs.google.com/spreadsheets/d/1aKcfX4u91aYFMPMOJBSlx92JmhRGH3MeVjLenxbEtAw/edit?usp=sharing
Below is my code:
function next_jam() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('PENALTY LINEUP ENTRY');
var Psheet = ss.getSheetByName('Penalties');
var Jam = sheet.getRange('D5');
var JamValue = Jam.getValue();
var Period = sheet.getRange('D2').getValue();
var H_J =sheet.getRange('B10').getValue();
var H_P =sheet.getRange('B13').getValue();
var H_B1 =sheet.getRange('B16').getValue();
var H_B2 =sheet.getRange('B19').getValue();
var H_B3 =sheet.getRange('B22').getValue();
var A_J =sheet.getRange('N10').getValue();
var A_P =sheet.getRange('N13').getValue();
var A_B1 =sheet.getRange('N16').getValue();
var A_B2 =sheet.getRange('N19').getValue();
var A_B3 =sheet.getRange('N22').getValue();
var H_Jp = sheet.getRange('C10:J11');
var H_JpValues = H_Jp.getValues();
var H_Pp = sheet.getRange('C13:J14');
var H_PpValues = H_Pp.getValues();
var H_B1p = sheet.getRange('C16:J17');
var H_B1pValues = H_B1p.getValues();
var H_B2p = sheet.getRange('C19:J20');
var H_B2pValues = H_B2p.getValues();
var H_B3p = sheet.getRange('C22:J23');
var H_B3pValues = H_B3p.getValues();
var A_Jp = sheet.getRange('O10:V11');
var A_JpValues = A_Jp.getValues();
var A_Pp = sheet.getRange('O13:V14');
var A_PpValues = A_Pp.getValues();
var A_B1p = sheet.getRange('O16:V17');
var A_B1pValues = A_B1p.getValues();
var A_B2p = sheet.getRange('O19:V20');
var A_B2pValues = A_B2p.getValues();
var A_B3p = sheet.getRange('O22:V32')
var A_B3pValues = A_B3p.getValues();
var H_P1 = Psheet.getRange('A1:H43');
var H_P2 = Psheet.getRange('AC1:AJ43');
var A_P1 = Psheet.getRange('P1:W43');
var A_P2 = Psheet.getRange('AR1:AY43');
var H_P1values = H_P1.getValues();
var H_P2values = H_P2.getValues();
var A_P1values = A_P1.getValues();
var A_P2values = A_P2.getValues();
// var ss = SpreadsheetApp.getActiveSpreadsheet();
// var sheet = ss.getSheetByName('PENALTY LINEUP ENTRY');
// var Psheet = ss.getSheetByName('Penalties');
// var Jam = sheet.getRange('E5').getValue();
// var Period = sheet.getRange('E2').getValue();
// var H_J =sheet.getRange('B10').getValue();
// var H_Jp = sheet.getRange('C10:J11').getValues();
// var H_P1 = Psheet.getRange('A4:H43');
// var H_P1values = H_P1.getValues();
Logger.log('This is the CURRENT HOME JAMMER = '+H_J);
for (var i = 0; i < 21; i++) {
var row = "";
for (var j = 0; j < 21; j++) {
if (H_P1values[i][j] == H_J && Period == 1) {
row = H_P1values[i][j+1];
Logger.log('This is the Row the HOME JAMMER is on = '+i); // This is your row number
I am trying to create an approval flow using sheet mail merge with approval buttons in the mail . Mail could generate when the user submits the HTML form.
Below script is not working I have attached the script with sample sheet.
I am not able to identify the exact issue, please help me to fix or identify
Sample Sheet
var REQUESTS_STATE_COLUMN = 32;
var STATE_APPROVERS_EMAILED = "EMAILED_Response Pending";
var STATE_APPROVED = "Approved";
var STATE_DENIED = "Rejected";
function sendEmail(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange("A2:AH2");
var data = dataRange.getValues();
for (i in data) {
var rowData = data[i];
var email = e.namedValues["Email Address"];
var emailAddress = rowData[30];
var submiterEmail = rowData[1];
var ProjectName = rowData[2];
var SrDesign = "Sr. Design Architect Services";
var PerHourRate = rowData[6];
var NumberOfHours = rowData[7];
var TotalRate = rowData[8];
var Contact = "Contact Centre Expert";
var PerHourRate2 = rowData[10];
var NumberOfHours2 = rowData[11];
var NTotalRate2 = rowData[12];
var NJrDesign = "Jr. Design Architect";
var PerHourRate3 = rowData[14];
var NumberOfHours3 = rowData[15];
var TotalRate3 = rowData[16];
var Outsourced = "Outsourced Technical Support Resource";
var PerHourRate4 = rowData[18];
var NumberOfHours4 = rowData[19];
var TotalRate4 = rowData[20];
var Supplier = "Supplier Management";
var PerHourRate5 = rowData[22];
var NumberOfHours5 = rowData[23];
var TotalRate5 = rowData[24];
var Project = "Project Management & Execution";
var PerHourRate6 = rowData[26];
var NumberOfHours6 = rowData[27];
var TotalRate6 = rowData[28];
var UserAccount = rowData[28];
var GrandTotalRate = rowData[29];
var seq = e.values[1];
var url = ScriptApp.getService().getUrl();
var sheet = SpreadsheetApp.openById('1i5x0oqpCDgOZO2Nbly4SpNkWpLbTXJhT6pPOfQ7NuEc').getSheetByName("Sheet1");
var cellval = range.getCell(row,1).getValue();
var options = '?approval=%APPROVE%&reply=%EMAIL%'
.replace("%EMAIL%",e.namedValues["User Email Address"])
var approve = url+options.replace("%APPROVE%","Approved")+'&row='+data+'&cellval='+cellval;
var reject = url+options.replace("%APPROVE%","Rejected")+'&row='+data+'&cellval='+cellval;
var subject = 'Technology Charges for '+ProjectName+'';
var html = '<p style="font-family:Verdana; fontsize=9px">Hello Recipient,<br/><br/><br/><br/>Referance No: '+cellval+'<br/><a href='+ approve +'> <b>approve </b></a><a href='+ reject +'> <b>Reject</b></a><br/>'
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: html,
});
//Add a state in the form spreadsheet with APPROVERS_EMAILED state
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
sheet.getRange(lastRow, REQUESTS_STATE_COLUMN+1).setValue(STATE_APPROVERS_EMAILED);
}
}
function doGet(e){
var answer = (e.parameter.approval === 'Approved') ? 'Yes -Approved!' : 'No-Rejected ';
var cellval = e.parameter.cellval;
var email = e.parameter.reply;
var sheet = SpreadsheetApp.openById("1i5x0oqpCDgOZO2Nbly4SpNkWpLbTXJhT6pPOfQ7NuEc").getSheetByName("Sheet1");
var data = sheet.getDataRange().getValues();
var headers = data[0];
var approvalCol = headers.indexOf("Approval") + 1;
if (0 === approvalCol) throw new Error ("Must add Approval column.");
// Record approval or rejection in spreadsheet
var row = ArrayLib.indexOf(data, 0, cellval);
if (row < 0) throw new Error ("Request not available."); // Throw error if request was not found
sheet.getRange(row+1, approvalCol).setValue(e.parameter.approval);
// Display response to approver
var app = UiApp.createApplication();
app.add(app.createHTML('<h2>Thank you..! Your response has been recorded'+' saying: '+ answer + '</h2>'))
return app
}
The getCell requires integer value for row and column but instead you are passing the entire row (rowData). You need to replace the line 45 to the below line.
var cellval = dataRange.getCell(i+1, 1).getValue()
I am trying to copy over all of the values and formats to a different spreadsheet, week by week, creating a new sheet within the workbook each time. This is for archival purposes. I have pieced together the following:
function ArchiveByWeek(){
var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CRS Stats By Week')
var buildVersion = Browser.inputBox("What is the Weekend Ending Date?");
var sValues = source.getDataRange().getValues();
var sBG = source.getDataRange().getBackgrounds();
var sFC = source.getDataRange().getFontColors();
var sFF = source.getDataRange().getFontFamilies();
var sFL = source.getDataRange().getFontLines();
var sFFa = source.getDataRange().getFontFamilies();
var sFSz = source.getDataRange().getFontSizes();
var sFSt = source.getDataRange().getFontStyles();
var sFW = source.getDataRange().getFontWeights();
var sHA = source.getDataRange().getHorizontalAlignments();
var sVA = source.getDataRange().getVerticalAlignments();
var sNF = source.getDataRange().getNumberFormats();
var sWR = source.getDataRange().getWraps();
var destination = SpreadsheetApp.openById('1WVfJGDbdOewO2H-aMhQiek7sCOolK6xH7cSbfZ8KQgY');
var destinationSheet = destination.insertSheet(buildVersion, 1);
destinationSheet.getRange(1,1,sValues.length,sValues[0].length).setValues(sValues)
.setBackgrounds(sBG)
.setFontColors(sFC)
.setFontFamilies(sFF)
.setFontLines(sFL)
.setFontFamilies(sFFa)
.setFontSizes(sFSz)
.setFontStyles(sFSt)
.setFontWeights(sFW)
.setHorizontalAlignments(sHA)
.setVerticalAlignments(sVA)
.setNumberFormats(sNF)
.setWraps(sWR);
}
This seems to work fine, only some cells are not copied. For some reason, it is only cells that are summing other cells. Can't figure out why.
I found the following script, which worked perfectly, only I was unable to find a way to give each new sheet a unique name:
function copySheetValues(){
var source = SpreadsheetApp.getActiveSheet();
var sourcename = source.getSheetName();
var sValues = source.getDataRange().getValues();
var sBG = source.getDataRange().getBackgrounds();
var sFC = source.getDataRange().getFontColors();
var sFF = source.getDataRange().getFontFamilies();
var sFL = source.getDataRange().getFontLines();
var sFFa = source.getDataRange().getFontFamilies();
var sFSz = source.getDataRange().getFontSizes();
var sFSt = source.getDataRange().getFontStyles();
var sFW = source.getDataRange().getFontWeights();
var sHA = source.getDataRange().getHorizontalAlignments();
var sVA = source.getDataRange().getVerticalAlignments();
var sNF = source.getDataRange().getNumberFormats();
var sWR = source.getDataRange().getWraps();
var destination = SpreadsheetApp.openById('15ucPbZrIYXZAOCYVdpK6OA0oyQT1NcsmuiJmDRfdpHQ');
var destinationSheet = destination.insertSheet(sourcename, 0);
destinationSheet.getRange(1,1,sValues.length,sValues[0].length).setValues(sValues)
.setBackgrounds(sBG)
.setFontColors(sFC)
.setFontFamilies(sFF)
.setFontLines(sFL)
.setFontFamilies(sFFa)
.setFontSizes(sFSz)
.setFontStyles(sFSt)
.setFontWeights(sFW)
.setHorizontalAlignments(sHA)
.setVerticalAlignments(sVA)
.setNumberFormats(sNF)
.setWraps(sWR);
}
I was actually able to come up with a solution that I was satisfied with. This was able to paste all of the values, and formats exactly as they were on the sheet when viewing. It asks for a name of the new sheet, which was my intention:
function Archive() {
var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SHEET NAME')
var sValues = source.getDataRange()
.getValues();
var weekEnding = Browser.inputBox("What is the Weekend Ending Date?");
var destination = SpreadsheetApp.openById('SHEET ID');
var copy = source.copyTo(destination)
.setName(weekEnding)
copy.getRange(1, 1, sValues.length, sValues[0].length)
.setValues(sValues); // overwrite all formulas that the copyTo preserved