Copying & pasting drop down lists clears cells - google-apps-script

So I'm quite new to script, and I've got some working dynamic dependent drop down lists. When I clear a cell in column A, it clears the cell directly to the right of it, which is great.
However, when I copy and paste a row, it clears the cell to the right of column A. My question is: Is there a way to make a row "copy & paste-able", so it doesn't clear column B when pasting?
Here are some images of what I'm describing:
[1]: https://i.stack.imgur.com/F0xOg.png
[2]: https://i.stack.imgur.com/geHuu.png
Lastly, here's the script that I'm using:
function onEdit(){
var tabLists = "Exercise Index";
var spreadsheet = SpreadsheetApp;
var activeSheet = spreadsheet.getActiveSpreadsheet().getActiveSheet();
var data = spreadsheet.getActiveSpreadsheet().getSheetByName(tabLists);
var activeCell = activeSheet.getActiveCell();
if(activeCell.getColumn() == 1 && activeCell.getRow() > 3 && activeSheet.getSheetName().includes("Week")){
activeCell.offset(0, 1).clearContent().clearDataValidations();
var makes = data.getRange(1, 1, 1, data.getLastColumn()).getValues();
var makeIndex = makes[0].indexOf(activeCell.getValue()) + 1;
if(makeIndex != 0){
var validationRange = data.getRange(2, makeIndex, data.getLastRow());
var validationRule = spreadsheet.newDataValidation().requireValueInRange(validationRange).build();
activeCell.offset(0, 1).setDataValidation(validationRule);
}
}
}
Copy of the google sheet: https://docs.google.com/spreadsheets/d/1OrDTJiAlVJRU5tKCA55CoVgnT83_k8n_yz9BkWYpZ_0/edit?usp=sharing
Thanks in advance!

From your replying, I thought that the reason for your current issue is due to activeCell.offset(0, 1).clearContent().clearDataValidations(). When your script is run, the right side cell of the active cell is cleared. So, in your script, how about the following modification?
From:
activeCell.offset(0, 1).clearContent().clearDataValidations();
To:
activeCell.offset(0, 1).clearDataValidations();
Or, remove activeCell.offset(0, 1).clearContent().clearDataValidations();.

Related

Dependent Dropdowns: 1 Independent, multiple dependent

So, this goal seemed simple at first, but I can't seem to wrap my head around how to accomplish it.
I have these independent dropdowns, with between 3 to 4 dependent dropdowns associated with each independent. I would like to be able to apply the associated dependent dropdowns when the independent dropdown is selected.
Example: Dropdowns in Column A have 3-4 variables associated with them (https://docs.google.com/spreadsheets/d/1ErRBqfEeRqTNLIH0N11IjViP-_ej1LMdrOXNvDOcsx8/edit?usp=sharing)
In the following code, I can apply 1 independent, and 1 dependent, but I can't figure out how to scale it to my needs:
function onEdit(){
var tabLists = "Lists";
var spreadsheet = SpreadsheetApp;
var activeSheet = spreadsheet.getActiveSpreadsheet().getActiveSheet();
var data = spreadsheet.getActiveSpreadsheet().getSheetByName(tabLists);
var activeCell = activeSheet.getActiveCell();
if(activeCell.getColumn() == 1 && activeCell.getRow() > 1 && activeSheet.getSheetName().includes("Programming")){
activeCell.offset(0, 1).clearDataValidations(); //Clears data validation 1 column over
var exerciseCat = data.getRange(1, 1, 1, data.getLastColumn()).getValues();
var catIndex = exerciseCat[0].indexOf(activeCell.getValue()) + 1;
if(catIndex != 0){
var validationRange = data.getRange(3, catIndex, data.getLastRow());
var validationRule = spreadsheet.newDataValidation().requireValueInRange(validationRange).build();
activeCell.offset(0, 1).setDataValidation(validationRule);
}
}
}
I thought that maybe my answer existed within making a 4-level dependent dropdown, but that didn't work either.
All help is greatly appreciated, as I am still quite new to any form of coding.
From your showing script, I guessed that you might have wanted to directly run the script with the script editor. So, in this modification, I didn't use the event object. By this, you can run the script with the OnEdit trigger and also directly run this modified script with the script editor.
And, in your script, only the data validation of column "B" of "Programming" sheet is set. In order to set other columns, I prepared an object for retrieving the values for setting the data validation rules.
So, how about the following modification?
Sample script:
function onEdit() {
var tabLists = "Lists";
var spreadsheet = SpreadsheetApp;
var activeSheet = spreadsheet.getActiveSpreadsheet().getActiveSheet();
var data = spreadsheet.getActiveSpreadsheet().getSheetByName(tabLists);
var activeCell = activeSheet.getActiveCell();
// I modified below script.
var row = activeCell.getRow();
if (activeCell.getColumn() == 1 && row > 1 && activeSheet.getSheetName().includes("Programming")) {
var obj = { "Top Sets": [1, 2, 3], "Top Set/Percentage": [4, 5, 6, 7] };
var v = activeCell.getValue();
if (!obj[v]) return;
activeCell.offset(0, 1, 1, activeSheet.getLastColumn() - 1).clearContent().clearDataValidations();
var lastRow = data.getLastRow();
var rules = obj[v].map(r => {
var r = data.getRange(3, r, lastRow - 2, 1);
return SpreadsheetApp.newDataValidation().requireValueInRange(r).build();
});
activeCell.offset(0, 1, 1, rules.length).clearContent().setDataValidations([rules]);
}
}
When the column "A" of "Programming" sheet is edited, this script is run. And, by the value of column "A", the data validation rules of other columns are put.
Note:
This modified script can be tested using your provided Spreadsheet. So, when you change the Spreadsheet, this script might not be able to be used. Please be careful about this.

Google Script - Unable to copy from one sheet to another

This is my first Google Script and I am coming from a VBA background so I have likely missed something obvious. I am attempting to copy from one sheet to another, some that is quite simple in VBA, but I am struggling with it and I dont understand what I am missing
The below code I would have though would copy a range in one sheet to another sheet. Not the whole range, just a section of it and only 1 row per an OnEdit()
I checked the ranges I am wanting to copy from and copy to using targetSheet.getRange(targetSheet.get1stNonEmptyRowFromBottom(1) + 1, 1,1,numColumns-1).activate(); & sheet.getRange(row, 1, 1, numColumns-1).activate(); and it selects the range I would expect
Currently the execution doesn't show any fails either
function onEdit(e) {
//This is sheet we will be copied to
const VALUE = ["LEAD","LANDSCAPE"];
//This is the column for the check box
const COLUMN_NUMBER = 25
//The main sheet that is being copied from
const MAIN_SHEET = 'Form Responses 1'
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();
var cell = sheet.getActiveCell();
var row = cell.getRow();
var lead_or_landscape = sheet.getRange(row,20).getValue();
//This is so we dont run this by mistake on another cell edit or sheet
if((COLUMN_NUMBER == cell.getColumn() && MAIN_SHEET == sheet.getName && cell.isChecked() == true)
&& (lead_or_landscape == VALUE[0] || lead_or_landscape == VALUE[1])){
//copies row from current sheet to the specificed sheet (Lead or landscape)
var numColumns = sheet.getLastColumn();
var targetSheet = spreadsheet.getSheetByName(lead_or_landscape);
var target = targetSheet.getRange(targetSheet.get1stNonEmptyRowFromBottom(1) + 1, 1,1,numColumns-1);
sheet.getRange(row, 1, 1, numColumns-1).copyTo(target());
}
}
Object.prototype.get1stNonEmptyRowFromBottom = function (columnNumber, offsetRow = 1) {
const search = this.getRange(offsetRow, columnNumber, this.getMaxRows()).createTextFinder(".").useRegularExpression(true).findPrevious();
return search ? search.getRow() : offsetRow;
};
You've to use parentheses in sheet.getName() and not use them in copyTo(target())
And you can use sheet.appendRow() method instead of copyTo(), that is easier.
function onEdit(e) {
//This is sheet we will be copied to
const sheets = ["LEAD","LANDSCAPE"];
//This is the column for the check box
const colNumber = 25;
//The main sheet that is being copied from
const mainSheet = 'Form Responses 1';
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();
var cell = sheet.getActiveCell();
var row = cell.getRow();
var leadOrLandscape = sheet.getRange(row,20).getValue();
//This is so we dont run this by mistake on another cell edit or sheet
if(colNumber == cell.getColumn()
&& mainSheet == sheet.getName()
&& cell.isChecked() == true
&& sheets.includes(leadOrLandscape)){
//copies row from current sheet to the specificed sheet (Lead or landscape)
var numColumns = sheet.getLastColumn();
var targetSheet = spreadsheet.getSheetByName(leadOrLandscape);
targetSheet.appendRow(sheet.getRange(row, 1, 1, numColumns-1).getValues()[0])
}
}
I just took a quick look at your code, are you sure about this line?
sheet.getRange(row, 1, 1, numColumns-1).copyTo(target());
it shouldn't rather be
sheet.getRange(row, 1, 1, numColumns-1).copyTo(target);

How to change the column the code is calling for in Google Sheet App script?

I'm using a app script code from: https://www.chicagocomputerclasses.com/google-sheets-apps-script-dynamic-dependent-dropdown-data-validation-lists/
to use dependent drop down data validation lists on a google sheet document.
The code is working but now I need to make it work on our office google sheet document. The code is made to work from the first column, but I need it to work on the E column of our document.
I've tried changing :
if(activeCell.getColumn() == 1 && activeCell.getRow() >1){
to : if(activeCell.getColumn() == 5 && activeCell.getRow() >1){
but it doesn't work, the code was working only on first column
and now it doesn't work at all, since I've been trying some things, even if it seems to me that I didn't change anything really beside the number of the column...
Here's my test google sheet: https://docs.google.com/spreadsheets/d/1p_bc6FsxiE8a0J8XElv6SyjnXXtwc0qjoJ_MuSkHcUg/edit#gid=1591147038
And here's the code :
function onEdit(){
var tablists = "Listes"
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var datass = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("tabLists");
var activeCell = ss.getActiveCell();
if(activeCell.getColumn() == 5 && activeCell.getRow() >1){
activeCell.offset(0, 1).clearContent().clearDataValidations();
var projets = datass.getRange(1, 1, 1, datass.getLastColumn()).getValues();
var projetsIndex = projets[0].indexOf(activeCell.getValue())+1;
if(projetsIndex ! = 0) {
var validationRange = datass.getRange(3, projetsIndex, datass.getLastRow());
var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
activeCell.offset(0, 1).setDataValidation(validationRule);
}
}
}
If you have any idea on what I'm missing !
Thanks a lot !!!
I think I just found out !
It was missing a semicolon on the second row of the code :
var tablists = "Listes"
Now it works !

Data Validation between workbooks in Google sheets

I've been having some trouble moving data from one source Spreadsheet to another, and having my formulas work. An overview of what I'm trying to do is have Spreadsheet 2 (the active document) pull information from Spreadsheet 1 (the source document). The code I made and was using before looked into a data validated dropdown list and then auto populated a new dropdown list. I want to automate the 1st data validated column and then have the same code run. I've put the code I'm having trouble with below. I am pretty new to this so any help would be much appreciated.
function onEdit(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var datass = SpreadsheetApp.openById("1APLcwkIE2EqVSS6Zongy799yg4z2tAN9J7h3jlJoBgQ").getSheetByName("Master List");
var activeCell = ss.getActiveCell();
var makes = datass.getRange(1, 1, 1, datass.getLastColumn()).getValues();
var makeIndex = makes[0].indexOf(activeCell.getValue()) + 1;
if(activeCell.getColumn() == 1 && activeCell.getRow() > 1) {
activeCell.offset(0, 1).clearContent().clearDataValidations();
if(makeIndex != 0) {
var validationRange = datass.getRange(2, makeIndex, datass.getLastRow());
var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
activeCell.offset(0, 1).setDataValidation(validationRule);
}
} }
So, I didn't find any script very easily but I found an add on that helped so much with this! Use SheetGo for anyone who has a similar issue, if your looking for an easy answer!

Google Sheets - Make sure cells have a value before executing script

I want to have a script execute only if all required cells contain information.
The above sheet shows how the layout is. The rows that need to be populated for the script to start are columns B to G
An extra feature that I would also like but is not required is if the user does not fill in all the cells required it will highlight them in red and delete the values in columns H & I of the active row in question.
Below is the script that executes when column H has the value "Completed"
function moveComplete() {
Utilities.sleep(200);
var sheetNameToWatch = "REQUEST";
var columnNumberToWatch = 9;
var valueToWatch = "COMPLETED";
var sheetNameToMoveTheRowTo = "ARCHIVE";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 2);
sheet.getRange(range.getRow(), 2, 1, sheet.getLastColumn()).setDataValidation(null).moveTo(targetRange);
sheet.deleteRow(range.getRow());
var lastrow = sheet.getLastRow();
sheet.insertRows(lastrow, 1);
}
}
You should trigger a checking (depending on what value you need) on all the cells that correspond as a required value with onEdit(). Afterwards, simply trigger your moveComplete() function to populate the rest of the other cells you need.
This Google Docs Help Forum post might give you an idea on how to do it. Just modify some of the code snippets that are already provided there. Other helpful docs:
Google AppScipt Triggers
Google AppScript Event Objects