getlastrow, then last column, and then populate based on that information - google-apps-script

Recently, there was a program by the name of Eddienuput that allows you to properly train against basically anything in training mode. One issue is that you have to make a text file with all the button presses, as well as the timing for each. Once this text file is made, the inputs are added into a virtual controller.
The working sheet can be seen here.
The idea here is that you put the data of the character on the second sheet, then on the generator sheet you select the buttons you want, then press the [>] to put it to the right of the current buttons stored, and you use the [v] button.
When I hit the down arrow (I've assigned the script to the buttons), the three entries needed for the program to register the input are inserted one row below the active row.
The problem is the Right arrow. I sort of have it right (it puts the three inputs needed to the right.. but it isn't finding the bottom-right entry and putting it to the right of it, but instead it looks for the furthest right column and puts it to the right of everything instead of next to the last cell on the last row.
here is the code as I have it now:
function addbuttontoString()
{
//DEFINE ALL ACTIVE SHEETS
var ss = SpreadsheetApp.getActiveSpreadsheet();
var eddGenSheet = ss.getSheetByName("Eddie Input Generator");
var lrwithcontentofEddGenSheet = eddGenSheet.getLastRow()+1;
var lcwithcontentofEddGenSheet = eddGenSheet.getLastColumn();
var CharacterFrameDataSheet = ss.getSheetByName("CharacterFrameData");
//Current string Variables
var totalcurrentstringRows = lrwithcontentofEddGenSheet-4;
var fullcurrentString = eddGenSheet.getRange(5,1,totalcurrentstringRows, lcwithcontentofEddGenSheet);
var cslastRow = fullcurrentString.getLastRow();
var bottomrightcolumnNumber = eddGenSheet.getRange(lrwithcontentofEddGenSheet,1,1,lcwithcontentofEddGenSheet).getLastColumn();
Logger.log(bottomrightcolumnNumber)
//GET LAST ROW #OF ITEM SHEET
var lastrowItem = CharacterFrameDataSheet.getLastRow();
// Get the perfect Eddie value from Character Frame Data you are grabbing data for
var button = eddGenSheet.getRange('B4').getValue();
// Set up the Perfect Frame Variable
for(var i = 2; i <= lastrowItem; i++)
{
if(button == CharacterFrameDataSheet.getRange(i, 1).getValue())
{
var Perfectframe = CharacterFrameDataSheet.getRange(i, 4).getValue();
}
}
Logger.log(fullcurrentString.isBlank());
if(fullcurrentString.isBlank()){
// POPULATE eddGen SHEET Same Line
eddGenSheet.getRange("A5").setValue(button);
eddGenSheet.getRange("B5").setValue("F");
eddGenSheet.getRange("C5" ).setValue(Perfectframe);
} else {
eddGenSheet.getRange(lrwithcontentofEddGenSheet-1, bottomrightcolumnNumber +1).setValue(button);
eddGenSheet.getRange(lrwithcontentofEddGenSheet-1, bottomrightcolumnNumber +2).setValue("W");
eddGenSheet.getRange(lrwithcontentofEddGenSheet-1, bottomrightcolumnNumber +3).setValue(Perfectframe);
}
}

I believe your goal as follows.
When you click the right arrow button, you want to add the values from the 1st empty column of the row.
In your script, in order to retrieve the coordinate of the column for putting values, getLastColumn() is used as follows. In this case, the 1st empty column of the row is not retrieved. The last column of the sheet is retrieved. So, how about the following modification?
From:
var bottomrightcolumnNumber = eddGenSheet.getRange(lrwithcontentofEddGenSheet,1,1,lcwithcontentofEddGenSheet).getLastColumn();
To:
var bottomrightcolumnNumber = eddGenSheet.getRange(lrwithcontentofEddGenSheet - 1, 1).getNextDataCell(SpreadsheetApp.Direction.NEXT).getColumn();
In this modification, the 1st empty column is retrieved using getNextDataCell().
Note:
I'm not sure about your actual situation. So when above modification was not useful for your situation, can you provide the detail information about your actual situation? By this, I would like to modify it.
References:
getLastColumn()
getNextDataCell(direction)

Related

Copy data range and paste to new page, and repeat

I am trying to copy a range from sheet 'Full' and paste the values only to a new sheet, 'Dump'. While the macro below does its action once, I am regenerating the original data range (Full), so I want to copy that new set and append to the same output page, indexed down to a blank row and keeping the first pasted data. Also then to do this 100 times.
The recoded macro is below, and I need to understand the script to add in to;
repeat the copy/paste function 100 times, and also
offset the paste range by a set number of rows.
Sorry, genuine newbie at editing google sheet macros. The Excel macro I use doesn't translate over.
Appreciate any answers you have.
function xmacro() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A1').activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Full'), true);
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Dump'), true);
spreadsheet.getRange('Full!BK3:BT34').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);```
};
Your macro is just an automatically generated app script. You can extend its functionality by building off that with some more code. First I'll explain some of the basic concepts, if you know this, then just skip down to the code.
Sheets Concepts
Here are some basic concepts that took me forever to figure out because most of the documentation assumes you are already proficient at Javascript.
A range is a 2 dimensional array that has one array for each row, and the contents of that array are the columns:
someRange = [
[row1Col1, row1Col2, row1Col3, row1Col4],
[row2Col1, row2Col2, row2Col3, row2Col4],
[row3Col1, row3Col2, row3Col3, row3Col4]
]
To access a specific value you need to reference the row array, and then the index of the column you want.
Think about it like hotel room numbers. The first part of the number is the floor,
and the second part is the specific room on that floor.
You access arrays by calling the array name, then square brackets with the index number of the element you want.
Arrays are indexed starting at 0, so to get row 1 you would use:
someRange[0] would return the inner array [row1Col1, row1Col2, row1Col3].
But that doesn't give you a specific cell values - so you would use a second set of brackets to access the column in that row:
someRange[0][1] = 'row1Col2'
Arrays also have built in information, so you can find the length of an array by using Array.length no parenthesis.
Since the rows are in the outer array, you can get the number of rows by seeing how many inner arrays there are.
someRange.length = 3 There are 3 row arrays in the someRange array.
You can do the same with columns, since the number of columns is equal to the number of elements in an array. To get the number of elements in the first row you would use:
someRange[0].length - which would be 4
And since a range has the same number of columns for each row, you can pick any row
to get the number of columns (generally, there are always exceptions)
The Code
The first function will create a custom menu item to run the code.
// create a new menu item for your custom function
function onOpen(){
SpreadsheetApp.getUi().createMenu()
.addItem('100 Copies', 'lotsOfCopies')
.addToUi();
}
function lotsOfCopies() {
var ss = SpreadsheetApp.getActive();
var copySheet = ss.getSheetByName('yourCopySheetName');
var pasteSheet = ss.getSheetByName('yourPasteSheetName');
// the range you wish to copy, change to fit your needs
var copyRange = copySheet.getRange('A1:B7');
var copyValues = copyRange.getValues();
var copyRows = copyValues.length;
var copyCols = copyValues[0].length;
// define the first row to be pasted into
var pasteRow = 1;
// define the left side column of the range to be pasted into
var pasteCol = 1
// build a loop that does the same thing 100 times,
// and each time offsets the paste range by the number of rows in the copy range
for (var i = 0; i < 100; i++) {
// for every iteration after the first,
// add the number of rows in the copy range to the variable 'row'
// example if there are 10 rows in the copy range then
// iteration 1 row = 1 Iterartion 2 row = 11, Iteration 3 row = 21
if (i > 0) {
pasteRow = +pasteRow + +copyRows
}
// build the range to paste into - it starts on pasteRow and paste col,
// and is as many rows as the copied range, and as many columns as the copied range
let pasteRange = pasteSheet.getRange(pasteRow, pasteCol, copyRows, copyCols);
// put the values from copyValues into the pasteRange
pasteRange.setValues(copyValues);
}
}
function xmacro() {
const ss = SpreadsheetApp.getActive();
const ssh = ss.getSheetByName('Full')
const dsh = ss.getSheetByName('Dump')
ssh.getRange('BK3:BT34').copyTo(dsh.getRange('A1'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}

Trying to limit an existing script to a specific sheet

My first issue is this, I have an items log sheet where I want to add and manage individual unique items in our inventory. I created a data validation dependent dropdown list for a main category and found out how to build a script to dynamically create a secondary category dropdown list based on the selected main category.
For Example:
If cell B2 (Main Category) is set to Carabiner (based on data validation range on another sheet) THEN cell C2 (secondary Category) will dynamically create a dropdown list relative to the Carabiner main category (i.e. locking, non-locking)
That is simple enough if you only have one row to create the dropdown lists, but I wanted to be able to pick from a secondary category list in each row dependent on which was picked in the main category cell.
I found a video of a script that did just that and got it working just fine.
Now the problem is that the script runs data validation on every other sheet. How can I limit the script to only run on a specific sheet?
Here is the script:
function onEdit() {
// this line just refers to the current file var start = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var current = start.getActiveCell()
// var to refer to the worksheets -lists is where the data validation range will come from, and main is where we want to use that data validation range var list = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Indirect_Categ_Ranges") var main = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Items_Log");
// has the user selected a category? Refers to the column number in the Items_Log sheet where the user has picked the main category
if (current.getColumn()==2)
{
//to copy the selected sub-category -the 2,1 is the row and column on the Indirect_Categ_Ranges sheet where this script will dynamically update the main category picked to define what the indirect function will display in the next column
var choice = current.getValue()
list.getRange(2,1).setValue(choice)
//clear any validation -the 2,3,1000 looks to start clearing validation at row 2, column 3 and down for up to 1000 entries
main.getRange(2,3,5000).clearDataValidations()
// create the rule - var_point defines the offset number of rows and columns to shift to initiate the dynamic dependent dropdown list, the var_items defines where the to look for the range to build the dropdown list
var point = current.offset(0,1)
var items = list.getRange(2,2,50)
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(items,true).build();
point.setDataValidation(rule)
}
}
Also, is there a way to have the clear validations to run no matter how many rows there are?
The function will run anytime there's an edit and there's nothing you can do to stop that. You can, instead, terminate execution preemptively if it's not the sheet you care about.
The event object tells you which range was edited. You can get that range's sheet to know which sheet was edited. If the name matches, then execute the other stuff.
function onEdit(e) {
if (e.range.getSheet().getName() === 'Items_Log') {
// Data validation
}
}
It's not great practice to use .getActiveRange() or .getActiveSheet() when you want what was actually edited because there is a chance, however small, that the edited range may differ from the active range at the time of function execution.
Explanation:
You need to take advantage of the event object.
That object contains relevant information to the edits you make.
For example:
e.source is equivalent to SpreadsheetApp.getActive()
e.range is equivalent to .getActiveCell().
To run the code only for a particular sheet, in this case Items_Log, add a condition to check if the name of the active sheet matches that name:
if (current.getColumn()==2 && start.getName()=="Items_Log")
where start is the active sheet:
var start = e.source.getActiveSheet();
Solution:
function onEdit(e) {
var start = e.source.getActiveSheet();
var current = e.range;
var list = e.source.getSheetByName("Indirect_Categ_Ranges")
var main = e.source.getSheetByName("Items_Log");
if (current.getColumn()==2 && start.getName()=="Items_Log")
{
var choice = current.getValue()
list.getRange(2,1).setValue(choice)
main.getRange(2,3,5000).clearDataValidations()
var point = current.offset(0,1)
var items = list.getRange(2,2,50)
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(items,true).build();
point.setDataValidation(rule)
}
}
Thanks for the help. after looking at your suggestions and trying a couple things, I found that simply adding the code:
&& start.getName()=="Items_Log")
To the end of the line:
if (current....
Worked and solved the issue.

How do I use a dropdown menu to change the cell contents of a range?

So I have a file on Sheets, and in the file is a section of cells that are formatted so when you enter "0", they are empty circles, and when you enter any other number (I just go with "1" to make it easy), the circles are filled. I know I could probably use the formulas in the actual sheet, but I'd like the option to edit the cells individually. I really only need the dropdown to fill them all at once or clear them all to save time. And the dropdown has options "Clear" and "Fill".
I have the dropdown menu in grouped cells AG97:AN97 and I'd like to be able to change the cells in W100:W103 (there are more ranges, but one step at a time.)
I've tried using previous questions to help me, but I can't seem to grasp the code.
function addValidation() {
var range = SpreadsheetApp.openById('(SHEETID)');
var sheet = ss.getSheets()[0];
var values = range.getValues();
var data = sheet.getDataRange().getValues();
var rule = SpreadsheetApp.newDataValidation().requireValueInList(["Clear", "Fill"]).build();
for(var i=0; i<data.length; i++) {
if(data[i][22] == "Clear") {
sheet.getRange(i+100, 7).clear().setDataValidation(rule);
}
}
}
I replaced the sheet idea just for the purpose of showing code. I tried to get the clear function to work before I tried working on fill, but no luck. I keep getting "Bad value (line 2, file "Code")"
"Bad value (line 2, file "Code")" means that you inserted your SpreadsheetID incorrectly.
If your Spreadsheet URl is something like
https://docs.google.com/spreadsheets/d/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/edit#gid=0, then the ID is the part designated with X - usualy 44 characters long.
Yuo need to pass it to the method openById() in quotes (or double quotes), but without brackets:
SpreadsheetApp.openById('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
Also, probably you want to assign it to a variable called ss rather than range, given your next line is var sheet = ss.getSheets()[0];.

Add up previous and current cell values when changed

I am trying to update 1 value in a Google sheet. When I select a cell which already contains a number (ex: 500), and I write a number (ex: 100), it should add them up without pressing a button (500+100).
Right now, when I edit the cell, the onEdit(e) will get triggered. I can then grab the cell's value by doing:
SpreadsheetApp.getActiveSpreadsheet().getActiveRange().getValue()
However, I cannot get the previous (before the change/edit) value, so I will always end up adding the two same values together. Here's some example code:
//Obviously only example code
var sheet = SpreadsheetApp.getActiveSpreadsheet();
function onEdit(e) {
if(activeRange.getColumn() == 3) {
var currentValue = sheet.getActiveRange().getValue();
var newValue = currentValue + sheet.getActiveRange().getValue();
sheet.getActiveRange().activeRange.setValue(oldValue);
}
}
Obviously I want to replace sheet.getActiveRange().getValue() in the newValue to the PREVIOUS value, otherwise I add both values up, which are the same. I hope you get what I mean. The previous value of the cell is never being sent/saved/logged anywhere.
In something like Javascript/jQuery, I would do this: When cell has been selected, save the current value in a variable. Then, when the cell has changed value, add the old value to the new value, and change the cell's value to be the newest added up value. Farely easy, but I cannot find anything about how to do this kind of trigger in Google Apps Script.
How can I possibly do this? Thanks!
"it should add them up without pressing a button" no that won't happen, but if you have 500 in Sheet1 cell C2 you need to duplicate it else where like i first commented - maybe C2 on another tab ( Sheet2 ). Then when you type 100 (and hit enter) you'll need to go and grab the old duplicated value(500), add it to the current edited value (100) then set the new calculated one(600):
function onEdit(e) {
if(activeRange.getColumn() == 3) {
var ss = e.source;
var sheet1 = ss.getActiveSheet();
var sheet2 = ss.getSheetByName("Sheet2");
var old = sheet2.getRange('C2').getValue(); // duplicated value
var current = sheet.getActiveRange().getValue(); // value after hitting enter to edit
var new = current + old;
sheet1.getActiveRange().setValue(new);
sheet2.getRange('C2').setValue(new); // update duplicated value
}
}
You'll need to handle matching up what rows to look at for duplicate values but for this example I just used row 2.

Clear Invalid Values from Spreadsheet

I'm using Google Spreadsheets for this:
I have a spreadsheet which is basically a 4-week planner. Each day is divided into several slots, which can be assigned to any of our active clients. These cells have validation rules which reject invalid values.
The data that is permitted by the validation rules is sourced from a list on a separate sheet, which filters out clients when their status is changed from 'Active' to 'Cancelled', meaning they can no longer be assigned. The status is changed manually. Once an assigned client changes to 'Cancelled', it becomes an invalid client on the calendar.
Is there a way, using scripts, to find and clear the values of cells containing these invalid values? I've included a screen clipping below. The red corner is the invalid value.
I already have the onEdit trigger set up to run code, this will be calling a function to deal with this specific area.
screen clipping
Any help will be appreciated.
The code would look something like this:
function onEdit(e) {
//First check if you want the entire code to execute
if (myNeededCondtion !=== "theValueToMach") {
//End the code here
return;
}
var mySpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var theSheet = mySpreadsheet.getSheetByName("name of sheet");
var arrayOfColumnValues = theSheet.getRange(row to start at, column to start at, numRows, numColumns).getValues();
var i=0;
var thisValue = "";
for (i=0;i<arrayOfColumnValues.length;i+=1) {
thisValue = arrayOfColumnValues[i][0];
if (thisValue==="Cancelled") {
//Set the cell value to a blank string
theSheet.getRange(i, column).setValue("");
};
};
};
You need to figure out what the range value parameters need to be, and edit the code. Add the correct sheet name to the getSheetByName method. Note that getValues() returns a two dimensional array. Each inner array represents a row. If you only get one column of data, then each inner array will only have one element in it. Arrays are indexed starting at zero.