Loop script whilst cell is false - google-apps-script

This formula is made for randomising football teams, I need it loop while cell AR20 is false.
how would I set this up?
function team1() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('AE18').activate();
spreadsheet.getCurrentCell().setValue('FALSE');
spreadsheet.getRange('AQ2').activate();
spreadsheet.getRange('AA2:AC14').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
spreadsheet.getRange('AL15').activate();
Tried while loop, keep getting the same error message.

Related

How to save on the next row from one existing data of copied google spreadsheet to another google spreadsheet using google apps script?

I have this dilemma where i recorded a macro to copy a row to another sheet but so happens every time i run the macro it will always save on the same row. what can I add to this script in order for the macro to save it after the previously saved data?
Current Code:
function SAVING2() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A2:O2').activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Audit RAW'), true);
spreadsheet.getRange('A2').activate();
spreadsheet.getRange('\'audit dump\'!A2:O2').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
What I wish to accomplish:
For the macro to save the next set of data after the previous
Your help would be greatly appreciated
In your script, from spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Audit RAW'), true); and spreadsheet.getRange('A2').activate();, the destination range is always "A2" of "Audit RAW" sheet. I think that this is the reason of your issue. In your situation, how about the following modification?
Modified script:
function SAVING2() {
var spreadsheet = SpreadsheetApp.getActive();
var src = spreadsheet.getRange('\'audit dump\'!A2:O2');
var dstSheet = spreadsheet.getSheetByName("Audit RAW");
var dst = dstSheet.getRange(dstSheet.getLastRow() + 1, 1);
src.copyTo(dst, SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
}
In this modification, the destination range is set as the next row of the last row. By this, the copied value is appended to the destination sheet.
Reference:
getLastRow()

How to make the cell reference in Google App Script dynamic?

I am creating a macro for my repetitive work.
What I want to do:
Copy a current cell in Sheet1
Paste it to Sheet2
There will be a =filter formula running at the bottom, and I need to navigate to the bottom and copy the range of outputs
Have the cell return back to its original A1 cell position in Sheet2
Paste the outputs to Sheet3, and move down one row
Loop it until it meet an empty cell in the row of Sheet1
Challenges I face:
In Sheet1: The macro I created only refer to the cell I first run the macro with (even after I start from a different cell, the macro still copying the same initial cell)
In Sheet2: The outputs will possibly be a row or multiple rows of output, so it seems like Ctrl+A during the macro may not quite do the work.
For Loop: The macro only me to run once, but I will need it run repetitively until it meets an empty cell in Sheet1.
Challenge 1 and 2 are my main challenges, I can manually use hotkeys to run the macro if I can't get the macro to loop, but will be definitely grateful if someone can teach me how to loop it.
function CleanUp6() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet2'), true);
spreadsheet.getRange('\'Sheet1\'!C2').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
var currentCell = spreadsheet.getCurrentCell();
spreadsheet.getActiveRange().getDataRegion().activate();
currentCell.activateAsCurrentCell();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet3'), true);
spreadsheet.getCurrentCell().offset(1, 0).activate();
spreadsheet.getRange('\'Sheet2\'!A8:D8').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
Using macro recorder to try to develop a script is not a good idea. It is recording mouse and keyboard actions, not code logic. I think what you want is something like this. Can't be sure but try it on a copy of your spreadsheet. Add a menu option with the onOpen() trigger.
function onOpen() {
var menu = SpreadsheetApp.getUi().createMenu("File Picker");
menu.addItem("Test","test");
menu.addToUi();
}
function test() {
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet();
if( sh.getName() !== "Sheet1") {
SpreadsheetApp.getUi().alert("Change to Sheet1");
return;
}
if( sh.getActiveCell().getA1Notation() !== "A1" ) {
SpreadsheetApp.getUi().alert("Change to cell A1");
return;
}
// get the values in A:A
var values = sh.getRange(1,1,sh.getLastRow(),1).getValues();
sh = ss.getSheetByName("Sheet2");
// copy values from Sheet1 to Sheet2
// assumes the formulas are outside of the range values
sh.getRange(1,1,values.length,1).setValues(values);
// get the last row
values = sh.getRange(sh.getLastRow(),1,1,sh.getLastColumn()).getValues();
sh = ss.getSheetByName("Sheet3");
// get the row following the last row .getLastRow()+1
sh.getRange(sh.getLastRow()+1,1,values.length,values[0].length).setValues(values);
}
catch(err) {
console.log(err);
}
}

Apply two scripts to a spreadsheet that has multiple sheets

I was able to find script that work for each sheet in a separate file, but can I have 2 scripts applied to a single file, but 2 sheets respectively? Essentially what I am after is this:
I am a pilot and I keep track of my logbook and flight times in a Google Sheets file. I have used this means for years but recently tried to apply some scripts to get to the first empty cell in column A as that is where I start with the date for each entry. The main reason for this is my sheet is now in excess of 3000 rows of entries. The first sheet in my file where I record my flights is titled "LOGBOOK". I have found a script to apply onOpen that will place my cursor to the first empty row of column A each time I open. Below is the script that I use. It works but takes approx 10 seconds to complete. If there is a faster means, I am open to suggestions. I only want to know the first empty cell in column A. This is what I use currently:
function onOpen() {
var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getRange("A:A").getValues();
var maxIndex = values.reduce(function(maxIndex, row, index) {
return row[0] === "" ? maxIndex : index;
}, 0);
sheet.setActiveRange(sheet.getRange(maxIndex + 2, 1));
}
Screen shot of Sheet 1, "LOGBOOK"
The second sheet in this file is titled "FLIGHT DUTY" In this sheet I want the same function to happen onOpen. I want the cursor to be placed in the first empty cell of column A as well. Again, I start column A with a date input for the given flight duty period. If I break this sheet out to a stand along Google Sheets file, this script works:
function onOpen(){
var sheet = SpreadsheetApp.getActiveSheet();
//Find the last cell with data in that specific column (A in this case)
var lastCell = sheet.getRange("A1").getNextDataCell(SpreadsheetApp.Direction.DOWN);
//Activate the next cell
lastCell.offset(1, 0).activate();
}
Screen shot of sheet 2 "Flight Duty"
In my current Google Sheets file, I want Sheet 1 "LOGBOOK" to onOpen, place my cursor to the first empty cell in Column A. At the same time, onOpen, I want Sheet 2 "FLIGHT DUTY" to place my cursor in the first empty cell of Column A of that sheet when I select that tab.
Below is a screen shot of the SHEET names at the bottom. The only sheets that I need a script for are 1 LOGBOOK and 2 FLIGHT DUTY.
Screen shot of Sheet Names
Here is a more stable version
function onOpen(){
var ui = SpreadsheetApp.getUi();
ui.createMenu('↓ Go to ... ↓')
.addItem('first empty row', 'goToFirstEmptyRow')
.addToUi();
var sh = event.source.getActiveSheet();
sh.getRange('A'+getFirstEmptyDataRow(sh)).offset(1,0).activate();
}
function goToFirstEmptyRow(event){
var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
sh.getRange('A'+getFirstEmptyDataRow(sh)).offset(1,0).activate();
}
function getFirstEmptyDataRow(sheet) {
var range = sheet.getRange("A4");
return range.getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
}
You have a menu at the top right
I tested the below code. The last row will be activated in as many sheets as you have. Whichever sheet you open, you will always land at the bottom row. Takes one second to run.
function onOpen() {
var ss = SpreadsheetApp.getActive();
var tabs= ss.getSheets();
for (i=0; i<tabs.length;i++){
var lr = tabs[i].getLastRow();
tabs[i].getRange("A"+(lr+1)).activate();
}
}
Try ...
function onOpen(event){
var sh = event.source.getActiveSheet();
sh.getRange('A'+getFirstEmptyDataRow(sh)).offset(1,0).activate();
}
function onSelectionChange(event){
var sh = event.source.getActiveSheet();
sh.getRange('A'+getFirstEmptyDataRow(sh)).offset(1,0).activate();
}
function getFirstEmptyDataRow(sheet) {
var range = sheet.getRange("A4");
return range.getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
}
https://docs.google.com/spreadsheets/d/1Um9gnfz3VH6Vf0PwNVW8LOD3F63qb5h7PQl-hSYT_0E/copy

Execution problem with button and trigger

I would admit that I'm not a pro at all, I just try to do some app script for my company and I faced a problem I really don't understand.
I can't execute a function I've made with a button nor with a trigger, but only by doing by my own with the execute button in the app script interface.
The issue is always the same :
Error The coordinates of the target range are outside the dimensions of the sheet.
But as I said above, when I execute by myself I don't receive any error message and the code works well.
Here is the code :
function Colle() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var lastRow = spreadsheet.getLastRow()+1;
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Data GC'), true);
spreadsheet.getRange('A1').activate
spreadsheet.getSheetByName("Data GC").getRange('A1:F500').activate(); // I know that my
//data will never exceed more than 400 so just as a security I've put 500
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sauvegarde GC'), true);
spreadsheet.getRange('A' + lastRow).activate();
spreadsheet.getSheetByName("Data GC").getRange('A1:F500').copyTo(spreadsheet.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
SuppDoubl(); //Delete the duplicates
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sauvegarde GC'), true);
spreadsheet.getRange('A1').activate();
};
*Sorry for my English
function SuppDoubl() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A:F').activate();
spreadsheet.getActiveRange().removeDuplicates().activate();
spreadsheet.getRange('A1').activate();
};
Why do you need so many activate()s? If you want just to copy some range from one sheet to other sheet it can be done without activate():
function colle() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName('Data GC').getRange('A1:F500');
var target = ss.getSheetByName('Sauvegarde GC').getRange('A1'); // or A + last row
source.copyTo(target, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
// SuppDoubl(); // <-- probably the error is here?
ss.setActiveSheet(ss.getSheetByName('Sauvegarde GC'), true);
ss.getRange('A1').activate();
}
It works fine for me (without the SupDoubl()) via button and any way.

CopyPasteType.ValuesOnly returns blank cell sometimes

I am trying to create a script that put a randbetween function into a cell and then copy/pastes it with values only.
I have two scripts that work separately, but when I try to combine them into one script it returns a blank cell.
Here is my first script to put the function into the cell. This puts the function into the cell, but the Values_only part doesn't seem to do anything as it leaves the function in the cell.
function letterDie() {
var ss = SpreadsheetApp.getActiveSheet();
var rand = "=index(Scategories!$A$15:$B$41,match(RANDBETWEEN(1,26),Scategories!$A$15:$A$41,0),2)"
var cell = ss.getRange('E1');
var letter = cell.setValue(rand).copyTo(cell, SpreadsheetApp.CopyPasteType.PASTE_VALUES);
}
Then here is the code I have that copies and pastes the function with values only so that I am left with just the random letter. This code also works fine.
function copyPaste() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('E1').activate();
spreadsheet.getRange('E1').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
I can run the first and then the second and it works fine, but when I try to combine them into one function, the cell is blank.
I have even tried creating a function to call the first, then the second but it still returns a blank cell.
function rollDie() {
letterDie();
copyPaste();
}
The issue is that both functions are making changes to the spreadsheet file and these changes depend on each other. Namely, letterDie() sets the value of cell E1 and copyPaste() gets that value.
Try to flush() the pending changes between the executions of the two functions:
function rollDie() {
letterDie();
SpreadsheetApp.flush();
copyPaste();
}