Autofill Script Google Spreadsheet Script - google-apps-script

I need you to help me out with this. I need a script on my Google Spreadsheet.
I need the script to do exactly what CTRL+A, CTRL+D does.
So it's auto filling the whole page based on the first row.
In terms of details.
Let's say there are two sheets, Sheet1 and Sheet2.
Now Sheet2 is a database. So every cell and row has plain value/text.
In Sheet1 I have the simplest formulas in each cell of row 1.
..Column 1....Column 2....Column 3...and so on
=Sheet2!A1, =Sheet2!B1, =Sheet2!C1,
So when you select the first row, grab the dot and drag down it will populate formulas into rows bellow and they will be like
=Sheet2!A1, =Sheet2!B1, =Sheet2!C1,
=Sheet2!A2, =Sheet2!B2, =Sheet2!C2,
=Sheet2!A3, =Sheet2!B3, =Sheet2!C3,
So when you press CTRL+A it will select the whole Sheet1 then when you press CTRL+D it will populate the formulas to every row in that worksheet.
My goal is to get script to be doing this.
So far I wrote
function f5(){
var sheet = SpreadsheetApp.getActiveSheet();
var allData = sheet.getDataRange();
var range = sheet.getRange(1,1,allData.getNumRows(),7)
range.activate();
};
Which selects the desired range but I don't think this is correct way going forward. I also was thinking of keypress simulation but can't get find any documentation saying that this can be done.

Something following should work:
var oneRowCopy = sheet.getRange(1,1,1, sheet.getLastColumn());
var targetRows = sheet.getRange(2,1,sheet.getLastRow()-1, sheet.getLastColumn());
oneRowCopy.copyTo(targetRows);

Related

Show a new row when something is written in the row above it

I have a predefined sheet where new employee names are generated, including their personal Info. I am looking for a script or fce, which can do this:
hide all empty rows;
once a value in cell (for example, A5) is entered -> new row is shown;
when a value is entered in the same cell on the new shown row (e.g., A6) -> another is shown, etc.;
And it all should be performed on a sheet called "JobWorkers"
Thank you very much.
Hide all the blank rows first by selecting the first row, then pressing the ctrl shift down arrow, then right click and select hide rows. Then you go to the script editor and have this onEdit function, that will see the last row, and unhide the next row.
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var lastRow = sheet.getLastRow();
sheet.showRows(lastRow+1);
}

How to easily split large Google sheet into separate sheets of 200 rows?

I frequently deal with large spreadsheets and am looking for a way to easily split these into rows of 200.
To explain more clearly: I have a spreadsheet containing one sheet (or tab?) with 2000 rows.
Currently, I would open a new sheet (in the same work book), mark the first 200 rows and copy and paste them into a new sheet. Then I mark the next 200 rows and copy and paste them into a new sheet etc.
Is there a way of automating this process or speeding it up with a function?
Thanks for your time and apologies for my poor explanation.
this script should do the trick, just replace the numberOfLines
function myFunction() {
var numberOfLines = 10;
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var yourNewSheet = {};
for (var i = 0; i < data.length; i++) {
var tabName = i%numberOfLines;
if(tabName == 0){
yourNewSheet = sheet.getParent().insertSheet("tab #" + i);
}
yourNewSheet.appendRow(data[i]);
}
}
You could use an ImportRange formula, like Chris Hick suggested here.
Or you may try using scripts. See more info here:
https://developers.google.com/apps-script/reference/spreadsheet/range
If you have no luck with this, please tell what you've tried so far so we could suggest any improvements.
Create a new sheet.
If your sheet has headers, click on cell A1 of your new sheet and enter =ARRAYFORMULA( -- click on your original sheet and click on the header row. The formula should complete to =ARRAYFORMULA(NameOfSheet!1:1)
In Cell A2 of your new sheet, enter =ARRAYFORMULA( -- click on your original sheet and select row 2. The formula should auto complete to =ARRAYFORMULA(NameOfSheet!2:2). You will want to modify the second number in the function to encompass as many rows as you wish to copy to the new sheet. For example, to copy 200 rows, you would use the formula =ARRAYFORMULA(NameOfSheet!2:202)
You can continue to create additional sheets -- simply duplicate the second sheet and augment the first and second numbers to copy additional sections of your sheet, for example:
=ARRAYFORMULA(NameOfSheet!203:402)
=ARRAYFORMULA(NameOfSheet!403:602), etc.

Delete Row in Google Sheet depending on other Google Sheet cell valuie

Is it possible to write a google script that will delete a row in a Google Sheet, based on cell values for a given range, in another google sheet?
I've done some research on how to do this all in the same google sheet, but my limited experience with writing google sheet scripts has prevented me from understanding if what I described is possible, and how.
This is a script I have so far. This will delete rows in a range of my active spreadsheet if a cell in Column F contains the word "id:123456". What I'd like to be able to do, is to modify this code so that if the word "id:123456" is found, it will look in another column of another Google Sheet, and delete rows that contain "id:123456".
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Sheet1'); // change to your own
var values = s.getDataRange().getValues();
for (var row in values)
if (values[row][6] == 'id:123456')
s.deleteRow(parseInt(row)+1);
};
I haven't tested this code, so you might need to do some modifications to run successfully. But the basic idea is simple: have two sheets open at once, and based on the if from the first sheet make changes in the second sheet.
var ss1 = SpreadsheetApp.getActiveSpreadsheet();
var ss2 = SpreadsheetApp.getByID(0); //Change to the other sheet's ID, or somehow open it.
var s1 = ss1.getSheetByName('Sheet1'); // change to your own
var s2 = ss2.getSheetByName('Sheet1'); // change to your own
var values = s1.getDataRange().getValues();
for (var row in values)
if (values[row][6] == 'id:123456')
var other_values = s2.getDataRange().getValues();
for (var other_row in other_values)
if (other_values[row][6] == 'id:123456')
s2.deleteRow(parseInt(other_row)+1);
Main thing to be worried about and test: I copied your delete code, but since we're deleting rows, unless we start from the bottom we might start missing. (Suppose rows 4 and 8 out of 10 contain something to be deleted; you might end up deleting 4, which turns 8 to 7, but then we still delete row 8--meaning that we've missed a row we should have deleted and deleted a row we shouldn't have.)

Google Sheets script to copy/paste from one sheet to another

Could someone help me with creating a script that does the following:
Function: Upon adding a new sheet to my already existing workbook, I would like it to copy Column "E" from Sheet 1 and (Paste Special > Conditional formatting only) to the newly introduced Sheet "X"
Where can I learn more on how to write code? I have never used Stackoverflow by the way someone just recommended me to come here. I believe I just answered my own question somehow on the post, which I am sure was wrong to do but I couldn't comment on an already existing answer without exceeding the limit.
// Adds custom menu
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('CustomMenu').addItem('Copy format', 'copyFormat') //Add function to menu.'GIVE NAME HERE', 'functionName'
.addToUi();
}
function copyFormat() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh1 = ss.getSheets()[0]; //Gets the first sheet of the workbook. Can use ss.getSheetByName('Name of sheet here'); If it is not the first sheet
var activeSh = ss.getActiveSheet(); //Get the active sheet, you should be on the sheet just added
var rowEnd = activeSh.getLastRow(); //Last row of active sheet
sh1.getRange("E1:E").copyFormatToRange(activeSh, 5, 5, 1, rowEnd); //Copy format, including conditional, to column E of active sheet
}
This just adds a button that allows you to select a cell and give it the same conditions has in the original sheet.
Example: Sheet 1: Column E1:E100 has a given condition.. which needs to apply in the exact same way to any new incoming sheets since they all come in the same format. Right now its at a point of which when a new sheet arrives in the workbook: I can enter the new sheet > Select the cell which requires the conditions > Select the custom menu > Cell gets conditioned. So the next step would be automate the process I just mentioned since there is several sheets added daily to the workbook.

google apps script - paste in next empty row

with google apps script I am collecting data from the google analytics API.
This is set to collect data every night 1 am for the previous day and writing that in to a sheet “original”
In my second script I want to copy a range A1:G1 from the sheet “original” to my second sheet “copy”
And what I want is continuously to copy the data to the next empty row, but how?
So far I have this as a basic solution, but this is always pasting the selected data into the defined cell (in this case cell A10)
function copytestdata() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("original");
var sheet2 = ss.getSheetByName("copy");
sheet1.getRange("A1:G1").copyTo(sheet2.getRange("A10"), {contentsOnly:true});
}
I would like a dynamic solution on the second sheet “copy”.
So new data should be pasted in to the next empty row starting from cell A1 and the go down to next empty row.
But how do I get the script to go down to the next empty row?
sheet1.getRange("A1:G1").copyTo(sheet2.[DYNAMIC SOLUTION]), {contentsOnly:true});
Have anyone got a brilliant idea? I would really appreciate the help.
Ohh and by the way how can I actually go to a specific cell sort of like:
sheet1.getActiveRange(“B8”)
Thanks a lot in advance for your help
Pelikan76
why not using getLastRow()+1 ?
function copytestdata() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("original");
var sheet2 = ss.getSheetByName("copy");
sheet1.getRange("A1:G1").copyTo(sheet2.getRange(sheet2.getLastRow()+1,1,1,7), {contentsOnly:true});
}