MCS
MCS

Reputation: 21

Google Apps Script to move row to another spreadsheet

I am using the script below to move rows based on the status to several tabs within the same spreadsheet. Instead of cutting and pasting into the A, B, and C tabs, I would like it to copy and paste the rows into completely different spreadsheet named A, B, and C.

function moveRows() {

    var ss = SpreadsheetApp.getActive();
    var source = ss.getSheets()[0];
    var val = source.getDataRange()
        .getValues();
    for (var row = val.length - 1; row >= 0; --row) {
        if (val[row][23] == 'A') {
            ss.getSheetByName('A')
                .appendRow(val[row]);
            source.deleteRow(parseInt(row) + 1);
        } else if (val[row][23] == 'B') {
            ss.getSheetByName('B')
                .appendRow(val[row]);
            source.deleteRow(parseInt(row) + 1);
        } else if (val[row][23] == 'C') {
            ss.getSheetByName('C')
                .appendRow(val[row]);
            source.deleteRow(parseInt(row) + 1);
        }
    }
}

Upvotes: 0

Views: 921

Answers (1)

Jesse Scherer
Jesse Scherer

Reputation: 1512

All you need to do extra is get a pointer to the right spreadsheets. I'll assume you do not know their Docs ID (the portion of the URL between /d/ and /edit). But, you know that the names are A, B, and so on. A snippet like this will get you a pointer to the right Sheets document(s), assuming there's only one document called A.

var files = DriveApp.getFilesByName('A');
var file = files.next();
var id = file.getId();
var ss = SpreadsheetApp.openById(id);

Otherwise continue as before, doing this once for every file you want to copy to.

Upvotes: 1

Related Questions