Hello so I am trying to get values from the 'book' sheet then copy it to 'Completed' sheet. It doesnt give any error but doesnt do the copying too. It takes the values form book stores it in the data array but doesnt copy them to the 'Completed' sheet. So I guess there is a problem with the last row.
var sheet = spreadsheet.getSheetByName('Book'); //source sheet
var Datarange = sheet.getRange('Q:Q'); //range to check
var Datavalue = (Datarange.getValues());
var dest = spreadsheet.getSheetByName('Completed'); //destination sheet
var data = [];
var j =[];
for (i=0; i<Datavalue.length;i++) {
if ( Datavalue[i] == "Completed") {
data.push.apply(data,sheet.getRange(i+1,1,1,24).getValues());
j.push(i);
}
}
dest.getRange(dest.getLastRow()+1,1,data.length,data[0].length).setValues(data); /// copy data to range
Something like this:
function whoknowswhat() {
var ss=SpreadsheetApp.getActive()
var sh=ss.getSheetByName('Book');
var rg=sh.getRange(1,17,sh.getLastRow(),1);
var v=rg.getValues();
var dest=ss.getSheetByName('Completed');
var data=[];
for (i=0; i<v.length;i++) {
if (v[i][?]=="Completed") {//need another parameter for column
data.push(v[i]);//v[i] is an entire row
}
}
dest.getRange(dest.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
Related
I have created a named ranged called MW_DATA in my sheet which covers the range U12:AA105
Based on the data present there could be a variable number of rows of data in U12:AA105 every time I copy the data to a Games_Log sheet and clear the data in the cells.
I need to improve the code so the filteredData variable is a dynamic range of rows each time (e.g. it could be U12:AA12 or U12:AA75)
current code:
function SaveMWData() {
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Games_Log");
var dataRange = dataSheet.getDataRange();
var dataLocation = dataRange.getLastRow();
var mwData = sheet.getRange("MW_DATA");
mwData.copyTo(dataSheet.getRange(dataLocation + 1,1),{contentsOnly:true});
}
Above code keeps copying all rows, including blank rows into the Games_Log sheet
Code I can't figure out:
function SaveMWData() {
var unfilteredData = sheet.getRange("MW_DATA");
var filteredData = ??
filteredData.copyTo(dataSheet.getRange(dataLocation + 1,1),{contentsOnly:true});
}
Filtered data would only copy over the rows with data
I believe I got it working as follow:
function SaveMWData() {
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Games_Log");
var dataRange = dataSheet.getDataRange();
var dataLocation = dataRange.getLastRow();
var row = sheet.getRange("MW_ROW").getValue();
var column = sheet.getRange("MW_COLUMN").getValue();
var numrows = sheet.getRange("MW_NUMROWS").getValue();
var numcolumns = sheet.getRange("MW_NUMCOLUMNS").getValue();
var mwData = sheet.getRange(row,column,numrows,numcolumns);
mwData.copyTo(dataSheet.getRange(dataLocation + 1,1),{contentsOnly:true});
}
Hello so I am trying to get values from the 'book' sheet then copy it to 'Completed' sheet. It takes the values form book stores it in the data array and copy them to the 'Completed' sheet. Its working fine but very slow which jeopardizes my work in time-wise. How can I make this run faster ?
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var clear_sheet = spreadsheet.getSheetByName('Completed_Orders'); // clear the destination sheet first
clear_sheet.getRange('A2:X').clear();
var sheet = spreadsheet.getSheetByName('book'); //source sheet
var Datarange = sheet.getRange('Q3:Q'); //range to check
var Datavalue = (Datarange.getValues());
var dest = spreadsheet.getSheetByName('Completed_Orders'); //destination sheet
var data = [];
for (i=0; i<Datavalue.length;i++) {
if ( Datavalue[i] == "Completed") {
data.push.apply(data,sheet.getRange(i+3,1,1,24).getValues());
}
}
dest.getRange(2,1,data.length,data[0].length).setValues(data);
var column = dest.getRange('A3:A');
var values = column.getValues(); // get all data in one call
var ct = 0;
while ( values[ct][0] != "" ) { // to find the last row correctly, getLastRow is not working perfectly
ct++;
}
var endRow = ct+2;
}
Try this:
Don't use this syntax var values=sheet.getRange('A3:A').getValues() because it get's the data all the way down to getMaxRows(). Instead use var values=sheet.getRange(3,1,sheet.getLastRow()-2,1).getValues().
Also Datavalue[i] is a whole row
function myfunction() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var clear_sheet = spreadsheet.getSheetByName('Completed_Orders');
clear_sheet.getRange(2,1,clear_sheet.getLastRow()-1,24).clear();
var sheet = spreadsheet.getSheetByName('book'); //source sheet
var Datarange = sheet.getRange(3,17,sheet.getLastRow()-2,1); //range to check
var Datavalue = Datarange.getValues();
var dest = spreadsheet.getSheetByName('Completed_Orders'); //destination sheet
var data = [];
for (var i=0;i<Datavalue.length;i++) {
if (Datavalue[i]["************You need another index here**************"] == "Completed") { //need another index Datavalue is 2d
data.push(sheet.getRange(i+3,1,1,24).getValues());
}
}
dest.getRange(2,1,data.length,data[0].length).setValues(data);
}
I think this version will be a lot faster.
function myfunction() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var ssh=ss.getSheetByName('book');
var dsh=ss.getSheetByName('Completed_Orders');
dsh.getRange(2,1,dsh.getLastRow()-1,24).clear();
var dv=ssh.getRange(3,1,ssh.getLastRow()-2,24).getValues();//this version just gets this data one time so it should a lot faster.
var data=[];
for (var i=0;i<dv.length;i++) {
if (dv[i][16]=="Completed") {
data.push(dv[i]);
}
}
dsh.getRange(2,1,data.length,data[0].length).setValues(data);
}
I am running this function that saves simulated data from another sheet. These ranges are the data that I want saved to the last row of another sheet. So for instance if I run the sim today 12/19 - I want the data to be appended to the last row after the sim data I ran yesterday 12/18.
Problems:
1.It doesn't copy the entire range of "B5:L6" it copy's C5:G6 or something like that
It doesn't append the rows downward, it moves the items down and copy's the last range to the first row IE 10,9,8,7 instead of 7,8,9,10
Any direction with this would be very much appreciated
function saveSimData() {
var ss = SpreadsheetApp.getActive();
var s = ss.getSheetByName('Avery Sim 2.0');
var destSheet = ss.getSheetByName("Test2");
var simDataList = s.getRangeList(['B5:L6', 'B7:L8','B9:L10','B11:L12','B13:L14','B15:L16','B17:L18','B19:L20','B21:L22','B23:L24','B25:L26','B27:L28','B29:L30','B31:L32','B33:L34']);
var simData = simDataList.getRanges();
var numGames = s.getRange('N10').getValue();
var lastRow = destSheet.getLastRow();
for (var i=0;i<numGames;i++){
destSheet.insertRowAfter(lastRow);
simData[i].copyTo(destSheet.getRange(lastRow +1,2), {contentsOnly:true});
}
}
function saveSimData() {
var ss=SpreadsheetApp.getActive();
var s=ss.getSheetByName('Sheet1');
var ds=ss.getSheetByName("Sheet2");
var simDataList=['B5:L6', 'B7:L8','B9:L10','B11:L12','B13:L14','B15:L16','B17:L18','B19:L20','B21:L22','B23:L24','B25:L26','B27:L28','B29:L30','B31:L32','B33:L34'];
var oA=[];
for(var i=0;i<simDataList.length;i++) {
var vA=s.getRange(simDataList[i]).getValues();//get data from each range
for(var j=0;j<vA.length;j++) {
oA.push(vA[j]);//append each row to output array
}
}
ds.getRange(ds.getLastRow()+1,5,oA.length,oA[0].length).setValues(oA);
}
or if you wish to use a RangeList instead:
function saveSimData() {
var ss=SpreadsheetApp.getActive();
var s=ss.getSheetByName('Sheet1');
var ds=ss.getSheetByName("Sheet2");
var sDL=s.getRangeList(['B5:L6', 'B7:L8','B9:L10','B11:L12','B13:L14','B15:L16','B17:L18','B19:L20','B21:L22','B23:L24','B25:L26','B27:L28','B29:L30','B31:L32','B33:L34']);
var sRL=sDL.getRanges();
var oA=[];
for(var i=0;i<sRL.length;i++) {
var vA=sRL[i].getValues();//get data from each range
for(var j=0;j<vA.length;j++) {
oA.push(vA[j]);//append each row to output array
}
}
ds.getRange(ds.getLastRow()+1,5,oA.length,oA[0].length).setValues(oA);
}
What I'm trying to do is within one spreadsheet, look at sheet "Source", if a cell in column B contains today's date, then copy cells between A:B for that cell and paste onto next empty row in sheet "Destination".
Repeat for all cells containing today's date.
This works if I'm looking for a text but essentially I'm trying to make this run automatically everyday when the date changes and I can't get the date function to work properly.
Essentially at the turn of each day it would copy today's data from another automated sheet, and paste it in the next row down from yesterday's data in another sheet.
I'm quite new to all of this and any help would be greatly appreciated.
function copy_test() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Source"); //Source sheet
var ds = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Destination"); //Destination sheet
var testrange = ss.getRange('B:B'); //Column to check for today's date
var testvalue = (testrange.getValues()); //What value to check
var today = Utilities.formatDate(new Date(), 'GMT-0', 'dd/MM/yyyy');
var data = [];
var j = [];
//Condition to check in B:B, if true, copy the same row to data array
for (i=0;i<testvalue.length;i++) {
if (testvalue[i] == today) {
data.push.apply(data,ss.getRange(i+1,1,1,3).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
ds.getRange(ds.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
The error I get is:
TypeError: Cannot read property "length" from undefined. (line 20, file "First Test")
Picture of the type of data I want to copy
Try this:
function copy_test() {
var ss=SpreadsheetApp.getActive();
var ssh=ss.getSheetByName("Source");
var dsh=ss.getSheetByName("Destination");
var rg=ssh.getRange(1,1,ssh.getLastRow(),3);//I just took the first three columns instead
var vA=rg.getValues();
var dt=new Date();
var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight
for(var i=0;i<vA.length;i++) {
var d=new Date(vA[i][1]);//column B
if (new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf() == today) {
dsh.appendRow(vA[i]);
}
}
}
In case anyone comes across this problem in the future. I messed around with the original code some more and managed to get it to work:
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Source'); //source sheet
var testrange = sheet.getRange('B:B');
var testvalue = (testrange.setNumberFormat("#").getValues());
var ds = ss.getSheetByName('Destination'); //destination sheet
var data = [];
var j =[];
var dt = new Date();
var today = Utilities.formatDate(new Date(), 'GMT-0', 'dd/MM/yyyy')
//Condition to check in B:B, if true, copy the same row to data array
for (i=0;i<testvalue.length;i++) {
if (testvalue[i] == today) {
data.push.apply(data,sheet.getRange(i+1,1,1,3).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
ds.getRange(ds.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
Specifically var testvalue where I added in setNumberFormat for the dates as text because that's the only way it would work for if (testvalue[i] == today)
Thanks #Cooper for taking a shot at this.
I have been searching all day trying to find out how to do this and get close but never to a finished state.
What I am trying to do is on active spreadsheet, look at Sheet "Happy". If a cell in Colum G contains "Closed" I want to copy the cells between range A:K for that row to sheet "Sad". Once the copy is done and the data is moved I want to then delete the whole row on sheet "Happy" that the data was copied from.
IF cell = closed, than copy to sheet and delete, else nothing.
I will run this script every 5 min which I can do with triggers.
Any help would be greatly appreciated.
Here is what I tried so far
function Copy() {
var sss = SpreadsheetApp.getActive()
var ss = sss.getSheetByName('Happy');
var range = ss.getRange('A:k');
var data = range.getValues();
var tss = SpreadsheetApp.getActive();
var ts = tss.getSheetByName('Sad:');
var valuesToCopy = ss.getRange(2,2,100).getValues();
ts.getRange(2,ts.getLastRows()+1,valuesToCopy.length,1).setValues(valuesToCopy);
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Happy'); //source sheet
var testrange = sheet.getRange('G:G');
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName('Sad'); //destination sheet
var data = [];
var j =[];
//Condition check in G:G; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == 'Closed') {
data.push.apply(data,sheet.getRange(i+1,1,1,11).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
//Delete matched rows in source sheet
for (i=0;i<j.length;i++){
var k = j[i]+1;
sheet.deleteRow(k);
//Alter j to account for deleted rows
if (!(i == j.length-1)) {
j[i+1] = j[i+1]-i-1;
}
}
}