I have a function which I got from online and modded slightly for my own use. Something I can not get working is how do I delete the full row and only copy over specific rows inside the function below.
I would like to be able to pass in variables to the function for each sheet and its corresponding columns to copy over.
ex. moveCompletedRows(type,whereToMoveType,[cell's to move]){}
function moveCompletedRows(type,whereToMoveType) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('SortBot');
var targetSheet = ss.getSheetByName(whereToMoveType);
var val = sheet.getDataRange().getValues();
var headers = val.shift();
var arr = [], rowsToWriteBack = [];
rowsToWriteBack.push(headers);
val.forEach(function (r, i) {
r[1] == type ? arr.push(r) : rowsToWriteBack.push(r)
});
if (arr.length > 0) {
targetSheet.getRange(targetSheet.getLastRow() + 1, 1, arr.length, arr[0].length)
.setValues(arr);
sheet.clear();
sheet.getRange(1, 1, rowsToWriteBack.length, rowsToWriteBack[0].length)
.clear()
.setValues(rowsToWriteBack);
}
}
Aside: The reason my current setup needs to change is since my form is so large (multi section form) submits alot of columns (A-CH) and each entry only submits around 8 each (just in different columns), even with hiding columns, when I view the pages on a mobile device it freezes/crashes 50% of the time(I am not sure this will fully solve it as the sheet with many rows will still be on that master sheet, note that it will be blank). Which makes me question if just having a sheet with that many cells that is never viewed on a mobile device cause the sheet to load very slow?
Thanks
Edit:
Link to the Drive folder containing the form/sheet/ script
https://drive.google.com/folderview?id=0B-7N8A7FPKw5V20yRVFQcmNZTlk&usp=sharing
Edit2:
Been trying to work on this the last few days and I have a secondary script I have semi working but part marked below is what I want to have working in the original formula
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet2Copy = ss.getSheetByName('Actions');
var targetSheet = ss.getSheetByName('Test');
var data = sheet2Copy.getRange(1,1, sheet2Copy.getLastRow(), sheet2Copy.getLastColumn()).getValues();
var dest = [];
for (var i = 0; i < data.length; i++ ) {
Logger.log(data[i][12]);// just to check if the condition is true sometimes ;-)
if (data[i][1] == "Action") {
var rowsToWriteBack = []; // initialise intermediate array
// GET THIS WORKING IN ABOVE FORMULA
// Trying to select columns to put into new sheet. Will eventually make the #'s variables passed into the formula to make it dynamic
rowsToWriteBack.push(
data[i][0],
data[i][1],
data[i][2],
data[i][10],
data[i][11],
data[i][12],
data[i][13]
);// choose here the columns you want to add
dest.push(rowsToWriteBack);
}
} // here is the end of the for loop
Logger.log(dest) ; // log the dest array instead
if (dest.length > 0 ) { // if something has been written in your array then batch write it to the dest. sheet
targetSheet.getRange(targetSheet.getLastRow()+1,1,dest.length,dest[0].length).setValues(dest);
}
}