How can i do Line Column with variable in it apps scripts - google-apps-script

this his the basic selecting cell Line by Column but i don't know how to put variable on it
function regle() {
var spreadsheet = SpreadsheetApp.getActive();
var i = 2
var u = 2
var p = spreadsheet.getRange(`L${i-1},C${u}`).getValue();
var t = spreadsheet.getRange(`L${i+1},C${u}`).getValue();
if (t == 1 && p == 1){
spreadsheet.getRange(`L${i}C${u}`).activate();
spreadsheet.getActiveRange().setValue(1);
}
else{
spreadsheet.getRange(`L${i}C${u}`).activate();
spreadsheet.getActiveRange().setValue(2);
}
}

Related

Change data validation column if values in another column equals to X or Y

In sheet 'Template', I have a data validation column B and I would like to change the value in this column, if value in column AB equals X or Y. Otherwise, I want the value in column B to be the same.
I found the following script which doesn't make much sense to me
function Test1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Template");
var numRows = SpreadsheetApp.getActiveSheet().getLastRow();
var range;
/* Loop through Column AB and find cells equal to order payment
and set Column B value based on it */
for (var i = 1; i <= numRows; i++) {
range = s.getRange('AB' + i );
if (range.getValue() == "X") {
range.offset(0, 6).setValue("X");
}
else {
range.offset(0,6).setValue("NO");
}
}
}
please check if this code works for you:
function Test1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Template");
// 27 = number of columns
var sheetContent = s.getRange(1,2,s.getLastRow(),27).getValues();
for (var i = 0; i < sheetContent.length; i++) {
if(sheetContent[i][26] == "X" || sheetContent[i][26] == "Y"){
//You can replace the updated value by anything you want it to be.
sheetContent[i][0] = "Updated Value";
}
}
s.getRange(1,2,sheetContent.length,sheetContent[0].length).setValues(sheetContent);
}
Try this:
function Test1() {
const ss = SpreadsheetApp.getActive();
const sh= ss.getSheetByName("Template");
const vs = sh.getRange(1,2,sh.getLastRow(),sh.getLastColumn()).getValues()
let a = vs.map((r,i) => {
if(r[27] == "X" || r[27] == "Y") {
return ["Yes"];
} else {
return [r[1]];
}
});
sh.getRange(1,2,a.length, a[0].length).setValues(a);
}

Avoid the same date in a column for maximum 10 times in Google Sheet

I would to find a way to avoid to insert in the column A of a spreadsheet the same date for maximum 10 times, and so if in the column A there is already the same date 10 times, if a user try to insert another time the same date, a popup will alert that it is not possible.
I am trying with Apps Script writing the code below, but it works only with alphabetical values and not with dates.
Maybe I have to format something?
function onEdit(e) {
var r = e.range;
var s = r.getSheet();
if(s.getName()==='Foglio1' && r.getColumn()===1) {
var newValue = e.value;
var b = s.getRange('A1:A');
var bv = b.getValues();
var count = 0;
var flag = false;
for(var i=0;i<bv.length;i++) {
if(bv[i][0]===newValue)
count++;
if(count>10) {
flag = true;
break;
}
}
if(flag) {
r.setValue(e.oldValue);
SpreadsheetApp.flush();
SpreadsheetApp.getUi().alert('This date is already inserted 10 times');
}
}
}
For example, as a simple modification, how about using getDisplayValue as follows?
Modified script 1:
function onEdit(e) {
var r = e.range;
var s = r.getSheet();
if (s.getName() === 'Foglio1' && r.getColumn() === 1) {
var newValue = r.getDisplayValue();
var b = s.getRange('A1:A');
var bv = b.getDisplayValues();
var count = 0;
var flag = false;
for (var i = 0; i < bv.length; i++) {
if (bv[i][0] === newValue) {
count++;
}
if (count > 10) {
flag = true;
break;
}
}
if (flag) {
r.setValue(e.oldValue);
SpreadsheetApp.flush();
SpreadsheetApp.getUi().alert('This date is already inserted 10 times');
}
}
}
Modified script 2:
As other method, in this modification, filter is used for counting the values.
function onEdit(e) {
var r = e.range;
var s = r.getSheet();
if (s.getName() === 'Foglio1' && r.getColumn() === 1) {
var newValue = r.getDisplayValue();
if (newValue == "") return;
var count = s.getRange('A1:A').getDisplayValues().filter(([a]) => a === newValue).length;
if (count > 10) {
r.setValue(e.oldValue);
SpreadsheetApp.flush();
SpreadsheetApp.getUi().alert('This date is already inserted 10 times');
}
}
}
References:
getDisplayValue()
getDisplayValues()
filter()

Checkboxes Triggering Addition Script in single row

In essence I'm trying to have a checkbox trigger addition. I have a number in Column A. When that number is entered checkboxes will appear in Column G. When that checkbox is checked I want it to trigger the addition function and add 1 to the same row in column A and not affect the other rows and then uncheck the checkbox.
So far, this is only working with Row 1 and not the other rows and I am not exactly sure why.
function onEdit (e) {
checkboxes(e);
addition(e)
}
function checkboxes(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ui = SpreadsheetApp.getUi();
var names = ss.getRange("A1:A");
var namesValues = names.getValues();
var checkboxes = ss.getRange("G1:G");
var cbRows = checkboxes.getHeight();
var cbValues = checkboxes.getValues();
var newCBValues = new Array(cbRows);
for (var row = 0; row < cbRows; row++) {
newCBValues[row] = new Array(0);
if (namesValues[row] == "" || namesValues[row] == " ") {
newCBValues[row][0] = " ";
}else{
if (cbValues[row][0] === true) {
newCBValues[row][0] = true;
}else{
newCBValues[row][0] = false;
}
}
}
checkboxes.setValues(newCBValues);
}
function addition(e) {
var ss = SpreadsheetApp.getActiveSheet();
var boxey = ss.getRange("G1:G")
var isAdd = boxey.getValue();
if (isAdd) {
Add();
boxey.setValue(false);
}
function Add() {
var ss = SpreadsheetApp.getActiveSheet();
var numbers = ss.getRange("A1:A");
numbers.setValue(numbers.getValue()+1);
}
Add one to column A of current row when checkbox in column G is checked
function onEdit(e) {
if (e.range.columnStart == 7 && e.value == "TRUE") {
const sh = e.range.getSheet();
sh.getRange(e.range.rowStart, 1).setValue(sh.getRange(e.range.rowStart, 1).getValue() + 1);
sh.getRange(e.range.rowStart, 7).setValue("FALSE")
}
checkboxes(e);//your current code
addition(e);//your current code
}

Google Script: When cell is filled, have prompt ask if user wants to run script

I have written a script to clean up and move data between 3 different sheets. A user first paste a data extract on to the "Extract" page, and then runs the script.
When a user paste the data into cell A1 on the "Extract" page, I would like a prompt box to ask the user if they would like to run the script; if Yes, run script, if No, don't run script and show message. How would I go about doing this?
This is what I have so far...everything after the first function works.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Extract");
if(ss.s().getRange("A1").getValue() != ""){
var ui = SpreadsheetApp.getUi()
var response = ui.alert('Would you jbot to find new claims for you?', ui.ButtonSet.OK_CANCEL);
if (response == ui.Button.OK) {
jbot();
} else {
Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.');
}
}
}
function jbot(){
movetobot();
deleteRows();
removeDupesInOtherSheets();
deleteCol();
cleanup();
movetoqueue();
message();
};
function movetobot() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Extract");
var ts = ss.getSheetByName("bot");
s.getRange("A1:BW").moveTo(ts.getRange("A1"));
}
function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("bot");
var r = s.getRange("BO:BO");
var v = r.getValues();
for(var i=v.length-1;i>=0;i--)
if(v[0,i]=='#POSTLESSEE' || v[0,i]=='#TDI CRC_OANKURA' || v[0,i]=='#Partpaymentoffer' || v[0,i]=='#TDI_CRC_DVANKURA' || v[0,i]=='#partpaymentdupe' )
s.deleteRow(i+1);
}
function removeDupesInOtherSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName("bot").getDataRange().getValues();
var s2 = ss.getSheetByName("Queue").getDataRange().getValues();
// iterate 'Queue' and check in 'bot' if duplicate values exist
var nS1 = [];
var s2Col1 = [];// data in column1 of Queue
for(var n in s2){
s2Col1.push(s2[n][0]);
}
for(var n in s1){ // iterate '180418970' and test col 1 vs col 1 in 'Queue'
var noDup1 = checkForDup(s1[n],s2Col1)
if(noDup1){nS1.push(noDup1)};// if not present in 'Queue' then keep
}
Logger.log(nS1);// view result
ss.getSheetByName("bot").getDataRange().clear();// clear and update sheets
ss.getSheetByName("bot").getRange(1,1,nS1.length,nS1[0].length).setValues(nS1);
}
function checkForDup(item,s){
Logger.log(s+' = '+item[0]+' ?')
if(s.indexOf(item[0])>-1){
return null;
}
return item;
}
function deleteCol() {
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("bot");
var lastCol = sheet.getLastColumn();
var keep = [1,2,3,4,45,53,74, 75]; // array of column numbers to keep
for (var col=lastCol; col > 0; col--) {
if (keep.indexOf(col) == -1) {
// This isn't a keeper, delete it
sheet.deleteColumn(col);
}
}
}
function cleanup() {
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("bot");
sheet.getRange("B1:B").moveTo(sheet.getRange("H1"));
sheet.deleteColumn(2)
sheet.deleteRow(1)
var cell = sheet.getRange("D:D");
cell.setNumberFormat("m/d/yy");
var cell = sheet.getRange("F:F");
cell.setNumberFormat("m/d/yy");
var cell = sheet.getRange("A:A");
cell.setHorizontalAlignment("center");
var cell = sheet.getRange("C:D");
cell.setHorizontalAlignment("center");
var cell = sheet.getRange("F:G");
cell.setHorizontalAlignment("center");
sheet.autoResizeColumn(2)
sheet.autoResizeColumn(5)
sheet.autoResizeColumn(7)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("bot");
var cell = sheet.getRange("BW1");
cell.setFormula("=COUNT(A:A)");
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("bot");
var range = sheet.getRange("A1:G");
range.sort({column: 4, ascending: true})
}
function movetoqueue() {
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("bot");
// skip row 1 (header) and beyond column G
var range = sheet.getRange(1,1,sheet.getLastRow()-1,7);
sheet = spread.getSheetByName("Queue");
var rows = sheet.getRange(1,1,sheet.getLastRow(),1).getValues();
// search for first blank row column 1
for( var i=0; i<rows.length; i++ ) {
if( rows[i][0] === "" ) {
var offset = sheet.getRange(1,1).offset(i,0);
range.copyTo(offset);
break;
}
}
}
function message() {
SpreadsheetApp.getActive().getSheetByName("Queue").activate();
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("bot");
var range = sheet.getRange("bot!BW1:BW1");
var data = range.getValue();
Browser.msgBox(data + " new claims have been added to the queue. Thank you for using jBot!");
}
Your onEdit(e) will trigger the alert regardless of which cell is edited. You need to check the event range to limit it to cell A1 of Extract like this.
function onEdit(e) {
if( e.range.getSheet().getName() === "Extract" ) {
if( e.range.getA1Notation() === "A1" ) {
if( e.value !== "" ) {
var ui = SpreadsheetApp.getUi();
// Do the rest of your stuff here
}
}
}
}

Set a Cell Value Based on Multiple Columns

I have the below script to set the cell value to "Y", when two columns from one sheet match two columns from another sheet, inside same spreadsheet. The 4 columns are not in the same position in their respective sheets. When I run the script it only puts one "Y" in a cell of the Cancel Reg column.
function cancelRegistration() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetR = ss.getSheetByName("Registration");
var sheetCR = ss.getSheetByName("Cancel Registration");
var dataR = sheetR.getDataRange().getValues();
var dataCR = sheetCR.getDataRange().getValues();
var headerRow = 1;
var headers = dataR[0];
var cellR = sheetR.getActiveCell();
var cellCR = sheetCR.getActiveCell();
var rowR = cellR.getRowIndex();
var rowCR = cellCR.getRowIndex();
for (var i = 1; i < dataR.length && i < dataCR.length; ++i) {
var eventIdR = rowR[1];
var emailR = rowR[7];
var eventIdCR = rowCR[1];
var emailCR = rowCR[4];
var urlCol = headers.indexOf("Cancel Reg");
if (eventIdR !== '' && emailR !== '') {
if (rowR[1] === rowCR[1] && rowR[7] === rowCR[4]) {
sheetR.getRange(headerRow + i, 10).setValue("Y");
}
}
}
}