Script to move entire row to a new tab and then selected columns to a different doc












0














I have the following script running on a document (I found on the forum and unfortunately I cannot remember who it was to give credit to but it works great), pulling the whole rows data into a new sheet then deleting the row from its current location;



function onEdit(event)
{
// assumes source data in sheet named Schedule
// target sheet of move to named Invoiced
// test column with "9 - Invoiced" is col 1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

if(s.getName() == "AllOpps" && r.getColumn() == 1 && r.getValue() ==
"Outreached")
{
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("InitialOutreach");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
var target = targetSheet.getRange(targetSheet.getLastRow()+1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}


What I am looking for is an amendment to this and I have no idea how to make it work as my script knowledge is extremely limited, in fact, I am not even sure if it is possible but here goes.



Upon selection of "Outreached" I would like that row to do 2 things, firstly for the whole row to be copied across to the InitialOutreach tab, as it does now, and at the same time column 11,12,13 and 14 to be copied into a completely different document, for ease of use I have added it in the shared sample doc as Mail. This second copy will need to be into the respectively named columns



You can find a sample sheet here. Please, note that AllOpps and Initial Outreach are in the same document and represents the initial transfer while Mail Merge tab will be the second transfer. Once again the second transfer ie. only select columns will be in a completely different document.
https://docs.google.com/spreadsheets/d/1x_NFLXC2doWAgCWcAbxpuAh1vW-IclMZ1prN4loWdDA/edit?usp=sharing.



I hope this is clear. Once again I am not even sure running 2 different tasks on 1 trigger is even possible but any efforts are highly appreciated.
Best,
J










share|improve this question
























  • This is doable and no problems with multiple tasks on 1 trigger. I started but got diverted so your test sheet has some extra test data.. If nobody else does it first, I will attend to it later today.
    – bcperth
    Nov 12 at 5:08
















0














I have the following script running on a document (I found on the forum and unfortunately I cannot remember who it was to give credit to but it works great), pulling the whole rows data into a new sheet then deleting the row from its current location;



function onEdit(event)
{
// assumes source data in sheet named Schedule
// target sheet of move to named Invoiced
// test column with "9 - Invoiced" is col 1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

if(s.getName() == "AllOpps" && r.getColumn() == 1 && r.getValue() ==
"Outreached")
{
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("InitialOutreach");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
var target = targetSheet.getRange(targetSheet.getLastRow()+1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}


What I am looking for is an amendment to this and I have no idea how to make it work as my script knowledge is extremely limited, in fact, I am not even sure if it is possible but here goes.



Upon selection of "Outreached" I would like that row to do 2 things, firstly for the whole row to be copied across to the InitialOutreach tab, as it does now, and at the same time column 11,12,13 and 14 to be copied into a completely different document, for ease of use I have added it in the shared sample doc as Mail. This second copy will need to be into the respectively named columns



You can find a sample sheet here. Please, note that AllOpps and Initial Outreach are in the same document and represents the initial transfer while Mail Merge tab will be the second transfer. Once again the second transfer ie. only select columns will be in a completely different document.
https://docs.google.com/spreadsheets/d/1x_NFLXC2doWAgCWcAbxpuAh1vW-IclMZ1prN4loWdDA/edit?usp=sharing.



I hope this is clear. Once again I am not even sure running 2 different tasks on 1 trigger is even possible but any efforts are highly appreciated.
Best,
J










share|improve this question
























  • This is doable and no problems with multiple tasks on 1 trigger. I started but got diverted so your test sheet has some extra test data.. If nobody else does it first, I will attend to it later today.
    – bcperth
    Nov 12 at 5:08














0












0








0







I have the following script running on a document (I found on the forum and unfortunately I cannot remember who it was to give credit to but it works great), pulling the whole rows data into a new sheet then deleting the row from its current location;



function onEdit(event)
{
// assumes source data in sheet named Schedule
// target sheet of move to named Invoiced
// test column with "9 - Invoiced" is col 1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

if(s.getName() == "AllOpps" && r.getColumn() == 1 && r.getValue() ==
"Outreached")
{
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("InitialOutreach");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
var target = targetSheet.getRange(targetSheet.getLastRow()+1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}


What I am looking for is an amendment to this and I have no idea how to make it work as my script knowledge is extremely limited, in fact, I am not even sure if it is possible but here goes.



Upon selection of "Outreached" I would like that row to do 2 things, firstly for the whole row to be copied across to the InitialOutreach tab, as it does now, and at the same time column 11,12,13 and 14 to be copied into a completely different document, for ease of use I have added it in the shared sample doc as Mail. This second copy will need to be into the respectively named columns



You can find a sample sheet here. Please, note that AllOpps and Initial Outreach are in the same document and represents the initial transfer while Mail Merge tab will be the second transfer. Once again the second transfer ie. only select columns will be in a completely different document.
https://docs.google.com/spreadsheets/d/1x_NFLXC2doWAgCWcAbxpuAh1vW-IclMZ1prN4loWdDA/edit?usp=sharing.



I hope this is clear. Once again I am not even sure running 2 different tasks on 1 trigger is even possible but any efforts are highly appreciated.
Best,
J










share|improve this question















I have the following script running on a document (I found on the forum and unfortunately I cannot remember who it was to give credit to but it works great), pulling the whole rows data into a new sheet then deleting the row from its current location;



function onEdit(event)
{
// assumes source data in sheet named Schedule
// target sheet of move to named Invoiced
// test column with "9 - Invoiced" is col 1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

if(s.getName() == "AllOpps" && r.getColumn() == 1 && r.getValue() ==
"Outreached")
{
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("InitialOutreach");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
var target = targetSheet.getRange(targetSheet.getLastRow()+1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}


What I am looking for is an amendment to this and I have no idea how to make it work as my script knowledge is extremely limited, in fact, I am not even sure if it is possible but here goes.



Upon selection of "Outreached" I would like that row to do 2 things, firstly for the whole row to be copied across to the InitialOutreach tab, as it does now, and at the same time column 11,12,13 and 14 to be copied into a completely different document, for ease of use I have added it in the shared sample doc as Mail. This second copy will need to be into the respectively named columns



You can find a sample sheet here. Please, note that AllOpps and Initial Outreach are in the same document and represents the initial transfer while Mail Merge tab will be the second transfer. Once again the second transfer ie. only select columns will be in a completely different document.
https://docs.google.com/spreadsheets/d/1x_NFLXC2doWAgCWcAbxpuAh1vW-IclMZ1prN4loWdDA/edit?usp=sharing.



I hope this is clear. Once again I am not even sure running 2 different tasks on 1 trigger is even possible but any efforts are highly appreciated.
Best,
J







google-apps-script google-sheets






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 4:18









AS Mackay

1,8554816




1,8554816










asked Nov 11 at 23:10









user10637952

105




105












  • This is doable and no problems with multiple tasks on 1 trigger. I started but got diverted so your test sheet has some extra test data.. If nobody else does it first, I will attend to it later today.
    – bcperth
    Nov 12 at 5:08


















  • This is doable and no problems with multiple tasks on 1 trigger. I started but got diverted so your test sheet has some extra test data.. If nobody else does it first, I will attend to it later today.
    – bcperth
    Nov 12 at 5:08
















This is doable and no problems with multiple tasks on 1 trigger. I started but got diverted so your test sheet has some extra test data.. If nobody else does it first, I will attend to it later today.
– bcperth
Nov 12 at 5:08




This is doable and no problems with multiple tasks on 1 trigger. I started but got diverted so your test sheet has some extra test data.. If nobody else does it first, I will attend to it later today.
– bcperth
Nov 12 at 5:08












1 Answer
1






active

oldest

votes


















0














NOTE: This reply has been modified from previous versions, to deal with various changes and problems as reported by the OP.



The code below should do what you need. Note carefully the changed function name myOnEdit which also appears in the onOpen event, where it is named as an "installable" trigger. See later notes on reason for this.



 function onOpen() {
ScriptApp.newTrigger('myOnEdit')
.onEdit()
.create();
}

function myOnEdit(event)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

if(s.getName() == "AllOpps" && r.getColumn() == 1 && r.getValue() == "Outreached")
{
var row = r.getRow();
var numColumns = s.getLastColumn();

// first copy and paste the "Outreached" row in entrity
var targetSheet = ss.getSheetByName("InitialOutreach");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
var target = targetSheet.getRange(targetSheet.getLastRow()+1, 1);
var values = s.getRange(row, 11, 1, 5).getValues(); // save the values to copy to "mail Merge"
s.getRange(row, 1, 1, numColumns).moveTo(target);
Logger.log(values);

// Copy the required columns to "Mail Merge" spreadsheet"
var mailMergeSS= SpreadsheetApp.openById('--copy your sheet ID here--');
targetSheet = mailMergeSS.getSheetByName("Mail Merge");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
target = targetSheet.getRange(targetSheet.getLastRow()+1,1,1,5);
target.setValues(values);

s.deleteRow(row);
}
}


1) Make the new target spreadsheet for the mail merge lines and make a new tag called "Mail Merge". Make



2) To find the ID of the new sheet open it and look at the URL. It should look something like this:
https://docs.google.com/spreadsheets/d/14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I/edit#gid=0



The ID is the emboldened bit ie 14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I in the example above,. Yours will be different, so copy and paste it so that your code line looks like:



var mailMergeSS= SpreadsheetApp.openById('14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I');


To avoid this error:




SpreadsheetApp.openById([0AjqSnE_p3nFqdDN0LWpFbjFqVDRwNmFGOV91QzZrZc])
[0 seconds] Execution failed: You do not have permission to perform
that action.




.. which arises when you try to open an external sheet, you need to use "Installable Triggers". The appropriate references are here:



www.developers.google.com/apps-script/guides/triggers/installable



developers.google.com/apps-script/guides/triggers/events



Installable triggers can be installed in 2 ways:




  1. In your CopyCols sheet, go to the script editor and select Current
    Project Triggers. Add a new trigger with settings as follows: Choose
    which function to run: myOnEdit Which runs at deployment:
    head Select Event Source: from Spreadsheet Select
    event type: onEdit Then save and restart your sheet..

  2. or....In your code add this function lines:


function onOpen() {
ScriptApp.newTrigger('myOnEdit')
.onEdit()
.create();
}






share|improve this answer























  • Hi there,I made a small error creating the sample sheet and forgot to enter the "Last Name" column on AllOpps and InitialOutreach sheets, hence the formating error. :-( I have added it in now, however, when copying across o Mail Merge it now needs to be columns 11,12,13,14 and 15? How to go about this?
    – user10637952
    Nov 12 at 10:37












  • OK no worries and I will fix it - stand by.
    – bcperth
    Nov 12 at 12:36










  • Ok is done, just had to change value of 4 to 5 in 2 places. Test it please and let me know if good.
    – bcperth
    Nov 12 at 12:47










  • that now works perfectly for the formatting and multiple tasks on 1 trigger. I did try and explain in my original post that the mail merge tab is in the sample sheet for ease of use, in reality, it is in a completely different doc. I have found something like this but do not know how to implement it in your script var newSheet = SpreadsheetApp.openById('sheet ID').getSheets()[0];
    – user10637952
    Nov 12 at 13:08










  • Ok I will take a look in the morning.. its 9:15PM here in Perth Australia...
    – bcperth
    Nov 12 at 13:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254172%2fscript-to-move-entire-row-to-a-new-tab-and-then-selected-columns-to-a-different%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














NOTE: This reply has been modified from previous versions, to deal with various changes and problems as reported by the OP.



The code below should do what you need. Note carefully the changed function name myOnEdit which also appears in the onOpen event, where it is named as an "installable" trigger. See later notes on reason for this.



 function onOpen() {
ScriptApp.newTrigger('myOnEdit')
.onEdit()
.create();
}

function myOnEdit(event)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

if(s.getName() == "AllOpps" && r.getColumn() == 1 && r.getValue() == "Outreached")
{
var row = r.getRow();
var numColumns = s.getLastColumn();

// first copy and paste the "Outreached" row in entrity
var targetSheet = ss.getSheetByName("InitialOutreach");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
var target = targetSheet.getRange(targetSheet.getLastRow()+1, 1);
var values = s.getRange(row, 11, 1, 5).getValues(); // save the values to copy to "mail Merge"
s.getRange(row, 1, 1, numColumns).moveTo(target);
Logger.log(values);

// Copy the required columns to "Mail Merge" spreadsheet"
var mailMergeSS= SpreadsheetApp.openById('--copy your sheet ID here--');
targetSheet = mailMergeSS.getSheetByName("Mail Merge");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
target = targetSheet.getRange(targetSheet.getLastRow()+1,1,1,5);
target.setValues(values);

s.deleteRow(row);
}
}


1) Make the new target spreadsheet for the mail merge lines and make a new tag called "Mail Merge". Make



2) To find the ID of the new sheet open it and look at the URL. It should look something like this:
https://docs.google.com/spreadsheets/d/14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I/edit#gid=0



The ID is the emboldened bit ie 14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I in the example above,. Yours will be different, so copy and paste it so that your code line looks like:



var mailMergeSS= SpreadsheetApp.openById('14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I');


To avoid this error:




SpreadsheetApp.openById([0AjqSnE_p3nFqdDN0LWpFbjFqVDRwNmFGOV91QzZrZc])
[0 seconds] Execution failed: You do not have permission to perform
that action.




.. which arises when you try to open an external sheet, you need to use "Installable Triggers". The appropriate references are here:



www.developers.google.com/apps-script/guides/triggers/installable



developers.google.com/apps-script/guides/triggers/events



Installable triggers can be installed in 2 ways:




  1. In your CopyCols sheet, go to the script editor and select Current
    Project Triggers. Add a new trigger with settings as follows: Choose
    which function to run: myOnEdit Which runs at deployment:
    head Select Event Source: from Spreadsheet Select
    event type: onEdit Then save and restart your sheet..

  2. or....In your code add this function lines:


function onOpen() {
ScriptApp.newTrigger('myOnEdit')
.onEdit()
.create();
}






share|improve this answer























  • Hi there,I made a small error creating the sample sheet and forgot to enter the "Last Name" column on AllOpps and InitialOutreach sheets, hence the formating error. :-( I have added it in now, however, when copying across o Mail Merge it now needs to be columns 11,12,13,14 and 15? How to go about this?
    – user10637952
    Nov 12 at 10:37












  • OK no worries and I will fix it - stand by.
    – bcperth
    Nov 12 at 12:36










  • Ok is done, just had to change value of 4 to 5 in 2 places. Test it please and let me know if good.
    – bcperth
    Nov 12 at 12:47










  • that now works perfectly for the formatting and multiple tasks on 1 trigger. I did try and explain in my original post that the mail merge tab is in the sample sheet for ease of use, in reality, it is in a completely different doc. I have found something like this but do not know how to implement it in your script var newSheet = SpreadsheetApp.openById('sheet ID').getSheets()[0];
    – user10637952
    Nov 12 at 13:08










  • Ok I will take a look in the morning.. its 9:15PM here in Perth Australia...
    – bcperth
    Nov 12 at 13:19
















0














NOTE: This reply has been modified from previous versions, to deal with various changes and problems as reported by the OP.



The code below should do what you need. Note carefully the changed function name myOnEdit which also appears in the onOpen event, where it is named as an "installable" trigger. See later notes on reason for this.



 function onOpen() {
ScriptApp.newTrigger('myOnEdit')
.onEdit()
.create();
}

function myOnEdit(event)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

if(s.getName() == "AllOpps" && r.getColumn() == 1 && r.getValue() == "Outreached")
{
var row = r.getRow();
var numColumns = s.getLastColumn();

// first copy and paste the "Outreached" row in entrity
var targetSheet = ss.getSheetByName("InitialOutreach");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
var target = targetSheet.getRange(targetSheet.getLastRow()+1, 1);
var values = s.getRange(row, 11, 1, 5).getValues(); // save the values to copy to "mail Merge"
s.getRange(row, 1, 1, numColumns).moveTo(target);
Logger.log(values);

// Copy the required columns to "Mail Merge" spreadsheet"
var mailMergeSS= SpreadsheetApp.openById('--copy your sheet ID here--');
targetSheet = mailMergeSS.getSheetByName("Mail Merge");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
target = targetSheet.getRange(targetSheet.getLastRow()+1,1,1,5);
target.setValues(values);

s.deleteRow(row);
}
}


1) Make the new target spreadsheet for the mail merge lines and make a new tag called "Mail Merge". Make



2) To find the ID of the new sheet open it and look at the URL. It should look something like this:
https://docs.google.com/spreadsheets/d/14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I/edit#gid=0



The ID is the emboldened bit ie 14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I in the example above,. Yours will be different, so copy and paste it so that your code line looks like:



var mailMergeSS= SpreadsheetApp.openById('14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I');


To avoid this error:




SpreadsheetApp.openById([0AjqSnE_p3nFqdDN0LWpFbjFqVDRwNmFGOV91QzZrZc])
[0 seconds] Execution failed: You do not have permission to perform
that action.




.. which arises when you try to open an external sheet, you need to use "Installable Triggers". The appropriate references are here:



www.developers.google.com/apps-script/guides/triggers/installable



developers.google.com/apps-script/guides/triggers/events



Installable triggers can be installed in 2 ways:




  1. In your CopyCols sheet, go to the script editor and select Current
    Project Triggers. Add a new trigger with settings as follows: Choose
    which function to run: myOnEdit Which runs at deployment:
    head Select Event Source: from Spreadsheet Select
    event type: onEdit Then save and restart your sheet..

  2. or....In your code add this function lines:


function onOpen() {
ScriptApp.newTrigger('myOnEdit')
.onEdit()
.create();
}






share|improve this answer























  • Hi there,I made a small error creating the sample sheet and forgot to enter the "Last Name" column on AllOpps and InitialOutreach sheets, hence the formating error. :-( I have added it in now, however, when copying across o Mail Merge it now needs to be columns 11,12,13,14 and 15? How to go about this?
    – user10637952
    Nov 12 at 10:37












  • OK no worries and I will fix it - stand by.
    – bcperth
    Nov 12 at 12:36










  • Ok is done, just had to change value of 4 to 5 in 2 places. Test it please and let me know if good.
    – bcperth
    Nov 12 at 12:47










  • that now works perfectly for the formatting and multiple tasks on 1 trigger. I did try and explain in my original post that the mail merge tab is in the sample sheet for ease of use, in reality, it is in a completely different doc. I have found something like this but do not know how to implement it in your script var newSheet = SpreadsheetApp.openById('sheet ID').getSheets()[0];
    – user10637952
    Nov 12 at 13:08










  • Ok I will take a look in the morning.. its 9:15PM here in Perth Australia...
    – bcperth
    Nov 12 at 13:19














0












0








0






NOTE: This reply has been modified from previous versions, to deal with various changes and problems as reported by the OP.



The code below should do what you need. Note carefully the changed function name myOnEdit which also appears in the onOpen event, where it is named as an "installable" trigger. See later notes on reason for this.



 function onOpen() {
ScriptApp.newTrigger('myOnEdit')
.onEdit()
.create();
}

function myOnEdit(event)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

if(s.getName() == "AllOpps" && r.getColumn() == 1 && r.getValue() == "Outreached")
{
var row = r.getRow();
var numColumns = s.getLastColumn();

// first copy and paste the "Outreached" row in entrity
var targetSheet = ss.getSheetByName("InitialOutreach");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
var target = targetSheet.getRange(targetSheet.getLastRow()+1, 1);
var values = s.getRange(row, 11, 1, 5).getValues(); // save the values to copy to "mail Merge"
s.getRange(row, 1, 1, numColumns).moveTo(target);
Logger.log(values);

// Copy the required columns to "Mail Merge" spreadsheet"
var mailMergeSS= SpreadsheetApp.openById('--copy your sheet ID here--');
targetSheet = mailMergeSS.getSheetByName("Mail Merge");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
target = targetSheet.getRange(targetSheet.getLastRow()+1,1,1,5);
target.setValues(values);

s.deleteRow(row);
}
}


1) Make the new target spreadsheet for the mail merge lines and make a new tag called "Mail Merge". Make



2) To find the ID of the new sheet open it and look at the URL. It should look something like this:
https://docs.google.com/spreadsheets/d/14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I/edit#gid=0



The ID is the emboldened bit ie 14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I in the example above,. Yours will be different, so copy and paste it so that your code line looks like:



var mailMergeSS= SpreadsheetApp.openById('14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I');


To avoid this error:




SpreadsheetApp.openById([0AjqSnE_p3nFqdDN0LWpFbjFqVDRwNmFGOV91QzZrZc])
[0 seconds] Execution failed: You do not have permission to perform
that action.




.. which arises when you try to open an external sheet, you need to use "Installable Triggers". The appropriate references are here:



www.developers.google.com/apps-script/guides/triggers/installable



developers.google.com/apps-script/guides/triggers/events



Installable triggers can be installed in 2 ways:




  1. In your CopyCols sheet, go to the script editor and select Current
    Project Triggers. Add a new trigger with settings as follows: Choose
    which function to run: myOnEdit Which runs at deployment:
    head Select Event Source: from Spreadsheet Select
    event type: onEdit Then save and restart your sheet..

  2. or....In your code add this function lines:


function onOpen() {
ScriptApp.newTrigger('myOnEdit')
.onEdit()
.create();
}






share|improve this answer














NOTE: This reply has been modified from previous versions, to deal with various changes and problems as reported by the OP.



The code below should do what you need. Note carefully the changed function name myOnEdit which also appears in the onOpen event, where it is named as an "installable" trigger. See later notes on reason for this.



 function onOpen() {
ScriptApp.newTrigger('myOnEdit')
.onEdit()
.create();
}

function myOnEdit(event)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

if(s.getName() == "AllOpps" && r.getColumn() == 1 && r.getValue() == "Outreached")
{
var row = r.getRow();
var numColumns = s.getLastColumn();

// first copy and paste the "Outreached" row in entrity
var targetSheet = ss.getSheetByName("InitialOutreach");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
var target = targetSheet.getRange(targetSheet.getLastRow()+1, 1);
var values = s.getRange(row, 11, 1, 5).getValues(); // save the values to copy to "mail Merge"
s.getRange(row, 1, 1, numColumns).moveTo(target);
Logger.log(values);

// Copy the required columns to "Mail Merge" spreadsheet"
var mailMergeSS= SpreadsheetApp.openById('--copy your sheet ID here--');
targetSheet = mailMergeSS.getSheetByName("Mail Merge");
if(targetSheet.getLastRow()+1 == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5);
//inserts 5 rows after last used row
}
target = targetSheet.getRange(targetSheet.getLastRow()+1,1,1,5);
target.setValues(values);

s.deleteRow(row);
}
}


1) Make the new target spreadsheet for the mail merge lines and make a new tag called "Mail Merge". Make



2) To find the ID of the new sheet open it and look at the URL. It should look something like this:
https://docs.google.com/spreadsheets/d/14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I/edit#gid=0



The ID is the emboldened bit ie 14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I in the example above,. Yours will be different, so copy and paste it so that your code line looks like:



var mailMergeSS= SpreadsheetApp.openById('14NP6TUyj1xLCPjKsPdGS6fiO0Bvzv1u4696LFd3tA7I');


To avoid this error:




SpreadsheetApp.openById([0AjqSnE_p3nFqdDN0LWpFbjFqVDRwNmFGOV91QzZrZc])
[0 seconds] Execution failed: You do not have permission to perform
that action.




.. which arises when you try to open an external sheet, you need to use "Installable Triggers". The appropriate references are here:



www.developers.google.com/apps-script/guides/triggers/installable



developers.google.com/apps-script/guides/triggers/events



Installable triggers can be installed in 2 ways:




  1. In your CopyCols sheet, go to the script editor and select Current
    Project Triggers. Add a new trigger with settings as follows: Choose
    which function to run: myOnEdit Which runs at deployment:
    head Select Event Source: from Spreadsheet Select
    event type: onEdit Then save and restart your sheet..

  2. or....In your code add this function lines:


function onOpen() {
ScriptApp.newTrigger('myOnEdit')
.onEdit()
.create();
}







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 at 0:11

























answered Nov 12 at 10:11









bcperth

2,0031514




2,0031514












  • Hi there,I made a small error creating the sample sheet and forgot to enter the "Last Name" column on AllOpps and InitialOutreach sheets, hence the formating error. :-( I have added it in now, however, when copying across o Mail Merge it now needs to be columns 11,12,13,14 and 15? How to go about this?
    – user10637952
    Nov 12 at 10:37












  • OK no worries and I will fix it - stand by.
    – bcperth
    Nov 12 at 12:36










  • Ok is done, just had to change value of 4 to 5 in 2 places. Test it please and let me know if good.
    – bcperth
    Nov 12 at 12:47










  • that now works perfectly for the formatting and multiple tasks on 1 trigger. I did try and explain in my original post that the mail merge tab is in the sample sheet for ease of use, in reality, it is in a completely different doc. I have found something like this but do not know how to implement it in your script var newSheet = SpreadsheetApp.openById('sheet ID').getSheets()[0];
    – user10637952
    Nov 12 at 13:08










  • Ok I will take a look in the morning.. its 9:15PM here in Perth Australia...
    – bcperth
    Nov 12 at 13:19


















  • Hi there,I made a small error creating the sample sheet and forgot to enter the "Last Name" column on AllOpps and InitialOutreach sheets, hence the formating error. :-( I have added it in now, however, when copying across o Mail Merge it now needs to be columns 11,12,13,14 and 15? How to go about this?
    – user10637952
    Nov 12 at 10:37












  • OK no worries and I will fix it - stand by.
    – bcperth
    Nov 12 at 12:36










  • Ok is done, just had to change value of 4 to 5 in 2 places. Test it please and let me know if good.
    – bcperth
    Nov 12 at 12:47










  • that now works perfectly for the formatting and multiple tasks on 1 trigger. I did try and explain in my original post that the mail merge tab is in the sample sheet for ease of use, in reality, it is in a completely different doc. I have found something like this but do not know how to implement it in your script var newSheet = SpreadsheetApp.openById('sheet ID').getSheets()[0];
    – user10637952
    Nov 12 at 13:08










  • Ok I will take a look in the morning.. its 9:15PM here in Perth Australia...
    – bcperth
    Nov 12 at 13:19
















Hi there,I made a small error creating the sample sheet and forgot to enter the "Last Name" column on AllOpps and InitialOutreach sheets, hence the formating error. :-( I have added it in now, however, when copying across o Mail Merge it now needs to be columns 11,12,13,14 and 15? How to go about this?
– user10637952
Nov 12 at 10:37






Hi there,I made a small error creating the sample sheet and forgot to enter the "Last Name" column on AllOpps and InitialOutreach sheets, hence the formating error. :-( I have added it in now, however, when copying across o Mail Merge it now needs to be columns 11,12,13,14 and 15? How to go about this?
– user10637952
Nov 12 at 10:37














OK no worries and I will fix it - stand by.
– bcperth
Nov 12 at 12:36




OK no worries and I will fix it - stand by.
– bcperth
Nov 12 at 12:36












Ok is done, just had to change value of 4 to 5 in 2 places. Test it please and let me know if good.
– bcperth
Nov 12 at 12:47




Ok is done, just had to change value of 4 to 5 in 2 places. Test it please and let me know if good.
– bcperth
Nov 12 at 12:47












that now works perfectly for the formatting and multiple tasks on 1 trigger. I did try and explain in my original post that the mail merge tab is in the sample sheet for ease of use, in reality, it is in a completely different doc. I have found something like this but do not know how to implement it in your script var newSheet = SpreadsheetApp.openById('sheet ID').getSheets()[0];
– user10637952
Nov 12 at 13:08




that now works perfectly for the formatting and multiple tasks on 1 trigger. I did try and explain in my original post that the mail merge tab is in the sample sheet for ease of use, in reality, it is in a completely different doc. I have found something like this but do not know how to implement it in your script var newSheet = SpreadsheetApp.openById('sheet ID').getSheets()[0];
– user10637952
Nov 12 at 13:08












Ok I will take a look in the morning.. its 9:15PM here in Perth Australia...
– bcperth
Nov 12 at 13:19




Ok I will take a look in the morning.. its 9:15PM here in Perth Australia...
– bcperth
Nov 12 at 13:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254172%2fscript-to-move-entire-row-to-a-new-tab-and-then-selected-columns-to-a-different%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values