I am trying to write an Google App Script for a google sheet that I have. The number of values I have in column D will change every week, and I am trying to copy those values to paste into another sheet.
So far, I can't find any code that will do the equivalent of selecting D2, and doing a Ctrl + Shift + down + copy.
function ELATerminationTransfer() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('D2').activate();SpreadsheetApp.getSelection().getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate};
Anyone know of a simple fix?
Try this:
function testSelection()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Source');
var destSheet = ss.getSheetByName('Destination');
// copy column values to destination sheet
sourceSheet
.getRange('D2:D')
.copyTo(destSheet.getRange('D2'), {contentsOnly:true});
}
You can select by this way
function myFunction(){
var cel = SpreadsheetApp.getActiveRange()
var end = cel.getNextDataCell(SpreadsheetApp.Direction.DOWN)
var rng = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(cel.getA1Notation()+':'+end.getA1Notation()).activate()
}
Related
I have found a script that pulls data from a cell and adds a row to the bottom of the Google Sheet, but I want to be able to have the output on a separate sheet called "Data". Here is the script:
function recordValue() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Channels");
var date = new Date();
var value =sheet.getRange("B9").getValue();
sheet.appendRow([date,value])
}
I know I need to change the sheet.appendRow but I don't know what I need to get it to another sheet, say in cell A2.
You must declare the second sheet.
function recordValue() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Channels");
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
var date = new Date();
var value = sheet.getRange("B9").getValue();
dataSheet.appendRow([date,value])
}
See:
The Modern JavaScript Tutorial
Outputting on a different sheet
function recordValue() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Channels");
const dsh = ss.getSheetByName("Data")
dsh.getRange(dsh.getLastRow() + 1, 1, 1,2),setValues([[new Date(),sh.getRange("B9").getValue()]]);
}
setValues
I really need your help. After 2 days, I'm still on "ZERO".
I need a script that copies all the formats from an active sheet to every other sheet.
It would be nice to have the conditional formatting, alternating colors and data validation too.
I think it's not so hard, but I'm really new in script writing.
This is the sheet:
https://docs.google.com/spreadsheets/d/1mOkm_r4rngPXTkIerSQJKxRih31sM7XoZCZVnoHUc5M/edit?usp=sharing
The code so far:
enter function copyFormatting(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var copyTo = ss.getSheetByName('agent2');
var range = copyTo.getRange(1, 1, copyTo.getMaxColumns(), copyTo.getMaxRows());
sheet.getBandings()[0].copyTo(copyTo.getRange(1,1)),SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
}
function allcopyFormatting() {
var spreadsheet = SpreadsheetApp.getActive();
var allSheets = spreadsheet.getSheets();
allSheets.forEach(function(sheet){
if(sheet.getSheetName() !== "detroit"){ // if you dont want to use the formatting on this sheet
sheet.activate();
copyFormatting(); // the function what yo like to run
}
})
}
I was able to figure out and write some functions: clear formatting on all sheets, format row headers on all sheets, show a list of sheets, but this one is too hard for me.
Even if I try to model it first with the macro recorder.
I would be very grateful, if you could help me with this issue.
I was able to figure it out:
function allcopyFormatting() {
var spreadsheet = SpreadsheetApp.getActive();
var allSheets = spreadsheet.getSheets();
var sampleSheet = spreadsheet.getActiveSheet(); // from the spreadsheet select the active sheet
var maxRow =sampleSheet.getMaxRows();
var maxCol =sampleSheet.getMaxColumns();
var sampleRange = sampleSheet.getRange(1,1,maxRow,maxCol);
allSheets.forEach(function(sheet){
if(sheet.getSheetName() !== "detroit"){ // if you dont want to use the formatting on this sheet
sheet.activate();
var targetSheet = spreadsheet.getActiveSheet(); // from the spreadsheet select the active sheet
var targetRange = targetSheet.getRange(1,1);
sampleRange.copyTo(targetRange, {formatOnly:true}) // it copies the formatting
}
})
}
Reference
Copy To
Try insertSheet(options) and if that doesn't work the try copyTo(destination, options)
Bit of a rookie at this and learning atm. I am using the following script to copy data from one sheet to another in the same Google spreadsheet:
function recordHistory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Cover");
var source = sheet.getRange("a1:c21");
var values = source.getValues();
var sheet2 = ss.getSheetByName("January 2020")
values [0] [0] = new Date();
sheet2.appendRow(values [0])
}
Which works fine to copy a single row but does not copy the entire range listed (a1:c21). Now I imagine the problem lies around Row 8 but I am not sure.
Here is the link to the spreadsheet I am using for trying this out:
https://docs.google.com/spreadsheets/d/15pEbNy-PmrGeAmpASRUK2iXIyJPfg43Mm2r6bbiTBuM/edit#gid=1296663899
function recordHistory() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Cover");
var source = sheet.getRange("a1:c21");
var values = source.getValues();
var sheet2 = ss.getSheetByName("January 2020")
values [0][0] = new Date();
sheet2.getRange(sheet2.getLastRow()+1,1,values.length,values[0].length).setValues(values);
}
i'm really newbie at scripting, i used to do my work with microsoft office that have a special paste "add" feature and now for google sheet i can't really find it.
I will have a source range of C2:C102 and destination at same sheet D2:D102 i want the script (that i can run manually weekly) to copy all the range from source and sum it with the already existing data at D2:D102 (only values).
Here is a small example - Before after
I tried to use this code but ofc it just replaces the values.
function copyCells(){
var thisSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var SourceSheet = thisSpreadsheet.getSheetByName("test");
var SourceRange = thisSpreadsheet.getRange("C2:C102");
var destinationSheet = thisSpreadsheet.getSheetByName("test");
var destinationRange = destinationSheet.getRange("D2:D102");
SourceRange.copyTo(destinationRange, {contentsOnly: true});
}
Any help will be really appreciated :)
Haven't tested the code but try this.
grab values with getValues()
sum the values
copy back values with setValues()
function copyCells(){
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const SourceSheet = spreadsheet.getSheetByName("test");
const SourceRange = spreadsheet.getRange("C2:C102");
const SourceValues = SourceRange.getValues();
const destinationSheet = spreadsheet.getSheetByName("test");
const destinationRange = destinationSheet.getRange("D2:D102");
const destinationValues = destinationRange.getValues();
for (let i = 0; i < SourceValues.length; i++)
destinationValues[i][0] = parseFloat(destinationValues[i][0]) + parseFloat(SourceValues[i][0])
destinationRange.setValues(destinationValues);
}
REFERENCES
range.getValues()
range.setValues()
I'm a new user of the Google Sheets and could use some help.
I have values in range A2:G2 on my currently active sheet. When the user clicks a button, I want those entered values to be copied into a 2nd sheet ('Sheet4') in the same main spreadsheet on the next open row.
I thought this would work, but the script is not finding the last row or adding the temptext values.
function button1(){
var activesheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var temptext = activesheet.getRange("A2:G2").getValue();
var activesheet2 = SpreadsheetApp.getActiveSpreadsheet();
activesheet2.setActiveSheet(activesheet2.getSheetByName('Sheet4'));
var lastrow = activesheet2.getLastRow(1);
activesheet2.getRange(lastrow + 1,1).setValue(temptext);
}
You need to get and set multiple values, and take care with the parameters to the functions (some take none).
function button1(){
var activesheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var temptext = activesheet.getRange("A2:G2").getValues();
var activesheet2 = SpreadsheetApp.getActiveSpreadsheet();
activesheet2.setActiveSheet(activesheet2.getSheetByName('Sheet4'));
var lastrow = activesheet2.getLastRow();
activesheet2.getActiveSheet().getRange(lastrow+1,1,1,7).setValues(temptext);
}