Reputation: 13
I have a script to move rows when a certain value appears in a given column, but I'd like to only move some of the columns within those rows.
this is the script I'm currently using:
//Move rows to Des tab from Form tab
function moveSafeRows()
{
var ss=SpreadsheetApp.getActive();
var sh0=ss.getSheetByName('Form');
var rg0=sh0.getDataRange();
var sh1=ss.getSheetByName('Des');
var vals=rg0.getValues();
for(var i=vals.length-1;i>0;i--)
{
if(vals[i][3]=='Yes')
{
sh1.appendRow(vals[i]);
sh0.deleteRow(i+1)
}
}
}
There are 8 columns in the Form tab, but I want to move only the values in columns 1,2,5.
I made a copy similar to the spreadsheet I'm working on, simulating the output I'd like to see in the second tab: https://docs.google.com/spreadsheets/d/17CRkz71FehikIblgwjHg-r982cUe95WLPI6LiFUr2LI/edit?pli=1#gid=0
Upvotes: 1
Views: 143
Reputation: 27348
You are very close.
You can just move the data only from columns 1, 2 and 5 by slicing the vals
array accordingly:
sh1.appendRow([vals[i][0],vals[i][1],vals[i][4]]);
Remember, 0
refers to the first column. Therefore, in our example, we are interested in 0, 1 and 4.
//Move rows to Des tab from Form tab
function moveSafeRows()
{
var ss=SpreadsheetApp.getActive();
var sh0=ss.getSheetByName('Form');
var rg0=sh0.getDataRange();
var sh1=ss.getSheetByName('Des');
var vals=rg0.getValues();
for(var i=vals.length-1;i>0;i--)
{
if(vals[i][3]=='Yes')
{
sh1.appendRow([vals[i][0],vals[i][1],vals[i][4]]);
sh0.deleteRow(i+1)
}
}
}
Upvotes: 1