google apps script - paste in next empty row - google-apps-script

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});
}

Related

Copying data from spreadsheet to another and to an active cell

Hope you guys are able to help me. I have been googling my eyes off...
I'm trying to copy data from one cell (B2) to another spreadsheet (the spreadsheet in use of course). I get the data, but it goes to cell A1 every time.
The cell that I copy the data to will be moving right every week, that is why I can't use a fixed cell.
Here is the code:
function a(){
var test = SpreadsheetApp.openById("mysourcespreadsheet").
getSheetByName("mysourcesheet").getRange("B2").getValues();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getCurrentCell();
cell.setValue(test);
}
The thing I really wanted to do is to copy the variable 'test' to the first empty cell in row 4, but let's just take on step at the time as I'm fairly new to the google scripts :)
Aaaargh... My stupidity hurts so much! I think I found the error. var sheet = ss.getSheets()[0]; -> should be var sheet = ss.getActiveSheet();
Thanks for the help thou

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.

How can I edit Google Spreadsheets Cells using the API?

I have a form and an API a Google Apps Script attached to it.
After sending the form I want to confirm that the script ran successfully with no errors by adding a value V to the last column of the form response's spreadsheet.
But I can't find any way to edit an existing row. Can someone help me with that?
Additional information from comments:
var responsesSSID = SpreadsheetApp.openById('*****************');
var values = responsesSSID.getDataRange().getValues();
values[values.length-1][values[].length-1] = "V";
I couldn't find anything here (for the next line) that will work.
Assuming that you want to update/edit the last column of the last row of your spreadsheet :
var sprdkey = '0Ai4sb45QfOAQdGhRWEtQaTR0enVHUHlxSTlobkVtenc';
var sheet = SpreadsheetApp.openById(sprdkey).getActiveSheet();
var lastRow = sheet.getLastRow();
var lastCol = sheet.getLastColumn();
sheet.getRange(lastRow , lastCol).setValue("V");
If you need to edit any other cell in the spreadsheet, you just need to pass the appropriate parameters to the getRange(row_no, col_no) function.
Hope this helps!

Writing variables to the next available spreadsheet row in a Google Apps Script

Quick question: I am a newbie to Google Apps Script, and want to do something really simple. I have a spreadsheet where I would like to log my results every time the script runs. Let's say I have three variables, and each time I execute, I want to go to the first available empty row in the sheet, and assign variable #1 to Column A, variable #2 to Column B, etc. I'm guessing there's all types of examples where that kind of thing happens every day. Can someone point me to such an example?
Thanks for your help.
TomC
maybe this way is better or at least shorter:
var logSheet = SpreadsheetApp.openById(logSpreadsheetId).getSheets()[0];
logSheet.appendRow([var1, var2, var3]);
In your Google Apps Script, you will want to look at something called sheet.getLastRow()
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// This logs the value in the very last cell of this sheet
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var lastCell = sheet.getRange(lastRow, lastColumn);
Logger.log(lastCell.getValue());
Source: https://developers.google.com/apps-script/reference/spreadsheet/sheet#getLastRow()

Autofill Script Google Spreadsheet 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);