2021年3月19日星期五

Copy Selected Columns in One Sheet and Add Them To Selected Columns in Another Sheet

I would like to create a simple google apps script to copy specific column into another sheets. Previously I tried using getLastRow but I get stuck to modify it.

var destinationSheetLastRow = destinationSheet.getDataRange().getLastRow();  

Here is my spreadsheet: https://docs.google.com/spreadsheets/d/1rGvmlKCmbjDSCLCC2Kujft5e4ngbSLzJd2NYu0sxISs/edit?usp=sharing

And here is the modified script so far:

function pasteMultiCol(sourceSheet, destinationSheet,sourceColumns,destinationColumns, doneColumn){       var sourceDataRange = sourceSheet.getDataRange();    var sourceDataValues = sourceDataRange.getValues();    var sourcesheetFirstRow = 0;    var sourceSheetLastRow = sourceDataRange.getLastRow();         var destinationSheetLastRow = destinationSheet.getDataRange().getLastRow();        var pendingCount = 0;        //Find the row start for copying    for(i = 0; i < sourceDataValues.length; i++){      if(sourceDataValues[i][doneColumn-1] === "Copied"){          sourcesheetFirstRow++;      };      if(sourceDataValues[i][doneColumn-1] === ""){          pendingCount++;      };    };        //Update Source sheet first row to take into account the header    var header = sourceSheetLastRow-(sourcesheetFirstRow + pendingCount);      sourcesheetFirstRow = sourcesheetFirstRow+header;        // if the first row equals the last row then there is no data to paste.     if(sourcesheetFirstRow === sourceSheetLastRow){return};        var sourceSheetRowLength = sourceSheetLastRow - sourcesheetFirstRow;        //Iterate through each column    for(i = 0; i < destinationColumns.length; i++){        var destinationRange = destinationSheet.getRange(destinationSheetLastRow+1,                                                         destinationColumns[i],                                                         sourceSheetRowLength,                                                         1);        var sourceValues = sourceDataValues.slice(sourcesheetFirstRow-1,sourceSheetLastRow);        var columnValues =[]        for(j = header; j < sourceValues.length; j++){          columnValues.push([sourceValues[j][sourceColumns[i]-1]]);        };        destinationRange.setValues(columnValues);    };        //Change Source Sheet to Copied.    var copiedArray =[];    for(i=0; i<sourceSheetRowLength; i++){copiedArray.push(["Copied"])};        var copiedRange = sourceSheet.getRange(sourcesheetFirstRow+1,doneColumn,sourceSheetRowLength,1)    copiedRange.setValues(copiedArray);         };     function runsies(){    var ss = SpreadsheetApp.openById("1snMyf8YZZ0cGlbMIvZY-fAXrI_dJpPbl7rKcYCkPDpk");    var source = ss.getSheetByName("Source");    var destination = ss.getSheetByName("Destination");          var sourceCols = [4,5,6,7,8,9,10];    var destinationCols = [7,8,9,10,11,12,13];        var doneCol = 12        //Run our copy and append function    pasteMultiCol(source,destination, sourceCols, destinationCols, doneCol);  };

Thanks in advance!

https://stackoverflow.com/questions/66717847/copy-selected-columns-in-one-sheet-and-add-them-to-selected-columns-in-another-s March 20, 2021 at 11:04AM

没有评论:

发表评论