Why is this copying to the new sheet at varying intervals? - google-apps-script

I'm copying data from one sheet to another 3x per week. The triggers are working fine but the data is supposed to copy to a new row sequentially. However, right now it's copying with varying numbers of rows in between them and I cant figure out why.
I have tried deleting all of the data in the sheet, deleting the sheet and recreating it, and double checked how I've input what data needs to be copied.
var logSheet = ss.getSheetByName('Sheet1');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Data Sheet');
var RDSheet = ss.getSheetByName('RawDataSheet');
var lastRow = logSheet.getLastRow();
var RawData = sourceSheet.getRange(45,1,1,34);
RawData.copyTo(RDSheet.getRange(lastRow + 1, 1), {contentOnly: false});
I expect the range (45,1,1,34), one long row, to copy itself to the RawDataSheet and when it triggers it places the data 1 row below the previously copied data.

Try this:
function myfunction() {
var ss=SpreadsheetApp.getActive();
var logSheet=ss.getSheetByName('Sheet1');
var sourceSheet=ss.getSheetByName('Data Sheet');
var RDSheet=ss.getSheetByName('RawDataSheet');
var lastRow=logSheet.getLastRow();
var RawData=sourceSheet.getRange(45,1,1,34);
RawData.copyTo(logSheet.getRange(lastRow + 1, 1), {contentOnly: false});
}

Related

copying not a scpecific number of rows

first I'm not good in coding just trying figure my way and make what i need.
so my function is to copy data from sheet to sheet and delete the data after finishing.
my issue is small but can't find a solution for it, it's when I'm copying it copy just 12 rows, but for me i want it to copy as much rows will be.
my final code i want it to copy the rows that have a specific word, but for now I'm searching for a solution for this issue.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet1");
var pasteSheet = ss.getSheetByName("Sheet2");
// get source range
var range = copySheet.getRange(2,1,12,4);
var source = range.getValues();
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,12,4).setValues(source);
range.clearContent();
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,12,1);
// clear source values
Browser.msgBox('Commande Confirmer');
}```
this is my code
thanks in advance
Description
In this example, the script gets all values from Sheet1, not just 12 rows. It then uses the Array.filter() to get all rows that have the word "Hello" in Column A. Not knowing how many rows match the filter I use source.length to tell getRange() how many rows to output.
Code.gs
function test() {
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet1");
var pasteSheet = ss.getSheetByName("Sheet2");
// get source range
var range = copySheet.getDataRange(); // Get all values
var source = range.getValues();
source = source.filter( row => row[0] === "Hello" );
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,source.length,source[0].length).setValues(source);
// clear source values
range.clearContent();
Browser.msgBox('Commande Confirmer');
}
catch(err) {
console.log(err);
}
}
Reference
Sheet.getDataRange()
Array.filter()

Sheet.hide() sometimes works sometimes not

this is my final script
function archive2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName('Lista'); // name of source sheet
var sourceRange = source.getRange('A1:B'); // range to copy
var destination1 = ss.getSheetByName('Sheet2'); // name of log sheet
var lastRow = destination1.getLastRow();
sourceRange.copyTo(destination1.getRange(5000 + 1, 1),{contentsOnly:true});
var source = ss.getSheetByName('Sheet2'); // name of source sheet
var sourceRange = source.getRange('H1:I5000'); // range to copy
var destination2 = ss.getSheetByName('Sheet3'); // name of log sheet
var lastRow = destination2.getLastRow();
destination2.getRange('A1:B').activate();
destination2.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
var source = ss.getSheetByName('Sheet2'); // name of source sheet
var sourceRange = source.getRange('H1:I5000'); // range to copy
var destination3 = ss.getSheetByName('Sheet3'); // name of log sheet
sourceRange.copyTo(destination3.getRange(1 + 1, 1),{contentsOnly:true});
var spreadsheet = SpreadsheetApp.getActive();
var source = ss.getSheetByName('Sheet3'); // name of source sheet
var sourceRange = source.getRange('A1:B5000'); // range to copy
var destination4 = ss.getSheetByName('Sheet2'); // name of log sheet
var lastRow = destination4.getLastRow();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet2'), true);
spreadsheet.getRange('A:B').activate();
spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
sourceRange.copyTo(destination4.getRange('A:B'),{contentsOnly:true});
var hide1 = ss.getSheetByName('Sheet2');
hide1.hideSheet();
var hide2 = ss.getSheetByName('Sheet3');
hide2.hideSheet();
}
Basically it makes a static copy of some data that I need to sum later. This part works fine.
What isn't working are the last lines, basically I want to hide Sheet2 and Sheet3, which are helper sheets to make the calcs.
The best way would be to make all the calcs without unhide those sheets but I wasn't able to find a solution (so if someone know what I've to change to perform it, would be great), so I'm searching a way to at least hide them after the script run.
With these lines, sometimes they are both hide, sometime just 1 of them (randomly between sheet2 and sheet3, I guess it have something to do with "active", and so I've tried to active both in sequence but still doesn't work).
Could you help me to hide both everytime (or make it works without unhide anything at all) without failures? Thanks
https://docs.google.com/spreadsheets/d/1GREsQNrDAaLqOROwAixajK6tpTRGOhqGHo0kfyyyuGw/edit#gid=0

Script for a button in google sheets

I created a button that does the following:
A. Copy a group of columns from sheet 1 (daily sports) and paste them into sheet 2 (Record).
B. The columns pasted in sheet 2 go to the next empty column (or create a new column) so they don't over-right the info that's already there..
C. Every time I'm done putting in new information I press the button and it automatically gets copied to sheet 2.
This is the code I'm using:
function moveValuesOnly () {
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Daily sports!B2:D20");
var destSheet = ss.getSheetByName("Record");
var destRange = destSheet.getRange(destSheet.getLastRow(),1);
var destRange = destSheet.getRange(destSheet.getLastColumn(),2);
source.copyTo (destRange, {contentsOnly: true});
source.clear ();}
The problems I'm having are as follows
1) Every time I hit the button it does not create a new column, instead it just covers the old columns.
2) the original source gets cleared (I know, It's the last function, it clearly says "clear" I just don't know what to write instead... I want the source to stay as is)
If anyone could help me with this script that would be great..
Thank you!
The following code should do what are you looking for:
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange ("Daily sports!B2:D20");
var destSheet = ss.getSheetByName("Record");
var row = 1; //the starting row of the range
var column = destSheet.getLastColumn()+1; //the starting column of the range
var numRows = 19; //the number of rows to return
var numColumns = 3; //the number of columns to return
var destRange = destSheet.getRange(row, column, numRows, numColumns);
source.copyTo(destRange, {contentsOnly: true})
}

Copy a range from last row to new sheet

I want to take the last response from a Google Form and copy it to the same range on another sheet.
The second sheet has some data in columns A & B (a login and password) that I want to assign as somebody completes the registration form.
I want the data from column B to H from the form response sheet to be copied to column C to I on the sheet that contains the login/password columns when a response is received.
I know I am missing the code to take the range from the last row, but I know NO coding at all and my current script is compiled from things I've found around the web and the reference material.
Any advice would be greatly appreciated.
function onEdit(e) {
var spreadsheet = SpreadsheetApp.openById("sheetid");
var sheet = spreadsheet.getSheets()[0];
var lastrow = sheet.getLastRow();
var range = sheet.getRange(2,2,1,7);
values = range.getValues();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SSsheet = ss.getSheets()[1];
var ssRange = SSsheet.getRange(2,3,1,7);
ssRange.setValues(values);
}
Try using copyTo(destination). It copies the data from a range of cells to another range of cells. Both the values and formatting are copied.
// The code below will copy the first 5 columns over to the 6th column.
var sheet = SpreadsheetApp.getActiveSheet();
var rangeToCopy = sheet.getRange(1, 1, sheet.getMaxRows(), 5);
rangeToCopy.copyTo(sheet.getRange(1, 6));
}
You can also try using copyValuesToRangecopyValuesToRange.
For additional code samples, you may also refer to this forum.
function onEdit(e) {
var spreadsheet = SpreadsheetApp.openById("ID");
var sheet = spreadsheet.getSheets()[0];
var lastRow = sheet.getLastRow();
var range = sheet.getRange("A"+(lastRow)+":G"+(lastRow));
var values = range.getValues();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SSsheet = ss.getSheets()[1];
var getRow = SSsheet.getLastRow() + 1;
var ssRange = SSsheet.getRange(getRow, 1, values.length, values[0].length);
ssRange.setValues(values);
Try with this,
this code copies the last value you want from a certain range and pastes it in the last available row of the other sheet.
The code it's the same only in the place where you are going to copy it, specify it to be the size of what I want to copy. I hope it helps you :D

Copy row to new sheet using Google Apps Script

I know it has been asked and answered here many times, but I can not make it work. I am trying to copy a row values (no formulas) from one sheet to another sheet on googlesheets using Google Apps Script. Very basic, but somehow it is giving me hard time.
I have:
SourceSheet
SourceWorksheet
TargetSheet
TargetWorksheet
I am trying to Copy row values (no formulas) from SourceWorksheet(A2:K2), to a new row (after last used row) on TargetWorksheet. It will be again from (A:K) but row number will be different, each time it will create a new row after last row.
Any help would be really appreciated!
EDIT
I found this code below, but not sure how to modify it to read the range i mentioned above instead of all the rows from source worksheet, and I need to modify it so it finds last row on target worksheet then creates new row each time to write the values ( so it doesn't overwrite any data on target)
function myFunction() {
var source = SpreadsheetApp.openById('xxxxxx');
var sourcesheet = source.getSheetByName('sheet1');
var target = SpreadsheetApp.openById('xxxxx')
var targetsheet = target.getSheetByName('sheet1');
var targetrange = targetsheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn());
var rangeValues = sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn()).getValues();
targetrange.setValues(rangeValues);
}
To add a line to the last row of a form, use the sheet.appendRow() method.
In your example, var targetRange would not be needed. You could simply use targetSheet.appendRow(rangeValues) where the rangeValues = the range you're copying (as an array)
function myFunction() {
var source = SpreadsheetApp.openById('xxxxxx');
var sourcesheet = source.getSheetByName('sheet1');
var target = SpreadsheetApp.openById('xxxxx')
var targetsheet = target.getSheetByName('sheet1');
var rangeValues = sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn()).getValues();
targetSheet.appendRow(rangeValues);
}