Google Sheets script to copy/paste from one sheet to another - google-apps-script

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.

Related

Google Apps Script to copy range in spreadsheet using sheet reference (not sheet name)

I need to copy a range in a spreadsheet from one row to another - but the name of the sheet changes and I am unable to work out how to use the sheet reference rather than sheet name when referring to the range.
Example code below which works - however I need to tell GAS to go get the first sheet, not sheet "S1"
function copyRange() {
var sheet = SpreadsheetApp.openById("1Ofua0Tzut5fnZRLkXwldy4pq4AVupdvNYybrFGrHjBU").getSheetByName("S1")
var source_range = sheet.getRange("A2:G2");
var target_range = sheet.getRange("A3:G3");
source_range.copyTo(target_range);
}
About Example code below which works - however I need to tell GAS to go get the first sheet, not sheet "S1", in this case, how about the following modification?
From:
var sheet = SpreadsheetApp.openById("1Ofua0Tzut5fnZRLkXwldy4pq4AVupdvNYybrFGrHjBU").getSheetByName("S1")
To:
var sheet = SpreadsheetApp.openById("1Ofua0Tzut5fnZRLkXwldy4pq4AVupdvNYybrFGrHjBU").getSheets()[0];
By this modification, the 1st tab is used as sheet. When [0] of getSheets()[0] is changed to [1], the 2nd sheets is used. When you changed the arangement of the sheet, the sheet is changed. For example, when you move the 1st tab from 3rd tab, getSheets()[0] retrieves the current 1st tab. Please be careful about this.
Reference:
getSheets()

Lock a sheet then export URL

Absolute noobie here, just trying to automate a process we currently do manually between google sheets.
I'd like to have a script attached to a button that does the following when pressed -
"lock" a vlookup or freeze a cell's contents. (I use a separate sheet of prices for materials that changes over time. When a quote gets accepted, I want to be able to "lock" the data that's already been pulled across from the other sheet, so the customer only pays what they've been quoted.
export the current sheet URL (or ID) to another sheet. I've got a "dashboard" sheet that we use as an overview of all the other sheets. Once the button is pressed I want to add the current sheet to the list of sheets we are monitoring on the dashboard sheet. Essentially I want to be able to extract the url of the sheet where we've just pressed the button, and add that url (or ID) to a list on another sheet.
Hope that makes sense! Any help is greatly appreciated.
This will get you started. The copyUrl function will paste the url to the first empty row in column B, you can change the column by changing the 2 on codeline 12.
function freeze(){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const activeSheet = ss.getActiveSheet();
activeSheet.getDataRange().copyTo(activeSheet.getDataRange(),{contentsOnly:true});
}
function copyUrl(){
const targetSheetId = 'xxxxxx';
const targetSheetName = 'sheetname';
const currentUrl = SpreadsheetApp.getActiveSpreadsheet().getUrl();
const target = SpreadsheetApp.openById(targetSheetId).getSheetByName(targetSheetName);
target.getRange(target.getLastRow()+1,2).setValue(currentUrl);
}
References
copyTo(spreadsheet)
getLastRow()

Copy Google sheet to a new sheet using cell value from 3rd sheet

I've been looking through the forums trying to search for the right way to do this but I can't seem to get it right. This is my first time using Google Apps Script so I'm learning as I'm going.
This is what I'm trying to do.
1) I want to duplicate all the contents of an old sheet into a new sheet. The script will determine the existing sheet name by referring to the cell b17 from 'Statistics' sheet, and will duplicate the old sheet, including values formats, references and formulas. The duplicated sheet will be named from cell c17 of the 'Statistics' sheet.
2) If possible I would also like to make the old sheet remove all the references and just keep the values and formats after the new sheet has been created. Similar to the copy all and ctrl-shift-v, except automating it in one fell swoop.
This is the code i currently have, not sure if it is correct
function newSheetLast() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("Statistics").activate();
var oldSheetName = ss.getRange('b17').getDisplayValue();
var newSheetName = ss.getRange('c17').getDisplayValue();
var sheet = ss.getSheetByName(oldSheetName).activate();
var source = SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet(newSheetName);
};
I am getting stuck when trying to do 1. I tried the copyTo and can get the sheet to copy, but loose all the formatting and formulas when i copy over to the new sheet.
I am not sure how to even attempt 2
You variable naming and the way duplicating sheets works is a bit confusing.
You should really be able to duplicate a Sheet Object directly or at least be able to try and supply the name of the sheet you are trying to duplicate but no, it is on a Spreadsheet level and only works on the active sheet...
Duplicating a sheet carries formulae and formatting.
Overwriting with static values can be done with setValues(getValues).
function newSheetLast() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var settings = ss.getSheetByName("Sheet1");
var oldSheetName = settings.getRange('B17').getDisplayValue();
var oldSheet = ss.getSheetByName(oldSheetName);
var newSheetName = settings.getRange('C17').getDisplayValue();
ss
.getSheetByName(oldSheetName)
.activate()
.getParent()
.duplicateActiveSheet()
.setName(newSheetName);
oldSheet.getDataRange().setValues(oldSheet.getDataRange().getValues());
};

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

copy cell values of named range in one sheet to same cell address of another named range in another sheet

Is it possible to copy the values of a named range in one sheet and paste them in the same cell location of another sheet?
For example, I have named range "old_desk_1" in sheet1!A24:A25 and named range "desk_1" in sheet2!A24:A25. Is it possible to copy the values in the cells of old_desk_1 to desk_1 without just doing the standard copy/paste?
Yes it is, first I suggest looking at the following documentations.
Google Class SpreadsheetApp
Google Class Spreadsheet
Google Class Sheet
Google Class Range
The above links will tell you about all the methods available for all the classes you will need to do this. Here is a basic example of what you're hoping to acheive.
function copyCells(){
var thisSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var theRange = thisSpreadsheet.getRangeByName("YOUR RANGE");
var destinationSheet = thisSpreadsheet.getSheetByName("SHEET TO COPY TO");
var destinationRange = destinationSheet.getRangeByName("YOUR OTHER RANGE");
theRange.copyTo(destinationRange);
}