In this blog post, we will explore how to use Google Apps Script to copy data from one Google Sheet to another. Specifically, we will copy five columns, excluding one column, and ensure that our destination sheet has an auto-incrementing number for the first column. Additionally, we will eliminate any duplicate rows in the process. This can be incredibly useful for organizing and cleaning up your data in Google Sheets.
Sample Data Table
Let’s start with a sample data table in a Google Sheet named SourceSheet
. This table has the following columns:
A | B | C | D | E | F |
---|---|---|---|---|---|
Name | Age | Occupation | Ignore | Country | Salary |
John | 25 | Engineer | Ignore1 | USA | 60000 |
Alice | 30 | Doctor | Ignore2 | UK | 70000 |
Bob | 22 | Student | Ignore3 | Canada | 20000 |
John | 25 | Engineer | Ignore4 | USA | 60000 |
Alice | 30 | Doctor | Ignore5 | UK | 70000 |
Goals
- Copy columns A, B, C, E, and F from
SourceSheet
toDestinationSheet
. - Add an auto-incrementing number as the first column in
DestinationSheet
. - Eliminate duplicate rows in
DestinationSheet
.
Google Apps Script Code
Below is the Google Apps Script code to accomplish this task:
function copyAndFilterData() {
// Open the spreadsheet and sheets
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName(‘SourceSheet’);
var destSheet = ss.getSheetByName(‘DestinationSheet’);
// Get the data from the source sheet
var sourceData = sourceSheet.getDataRange().getValues();
// Extract the relevant columns (A, B, C, E, F)
var filteredData = sourceData.map(function(row) {
return [row[0], row[1], row[2], row[4], row[5]];
});
// Remove header row
var header = filteredData.shift(); // Remove header row
// Remove duplicates
var uniqueData = removeDuplicates(filteredData);
// Add auto-incrementing numbers as the first column
var numberedData = uniqueData.map(function(row, index) {
return [index + 1].concat(row);
});
// Add header to the numberedData
numberedData.unshift([‘ID’].concat(header));
// Clear the destination sheet and set the new data
destSheet.clear();
destSheet.getRange(1, 1, numberedData.length, numberedData[0].length).setValues(numberedData);
}
function removeDuplicates(data) {
var seen = {};
return data.filter(function(row) {
var key = row.join(‘-‘);
return seen.hasOwnProperty(key) ? false : (seen[key] = true);
});
}
Explanation
- Extract the Relevant Columns:
var filteredData = sourceData.map(function(row) { return [row[0], row[1], row[2], row[4], row[5]]; });
This ensures that we extract the necessary columns: A, B, C, E, and F. - Remove Header Row:
var header = filteredData.shift();
This removes the header row from the filtered data and stores it separately. - Remove Duplicates:
var uniqueData = removeDuplicates(filteredData);
This ensures that any duplicate rows are removed. - Add Auto-Incrementing Numbers:
var numberedData = uniqueData.map(function(row, index) { return [index + 1].concat(row); });
This adds an auto-incrementing number as the first column for each row in the unique data. - Add Header to Numbered Data:
numberedData.unshift(['ID'].concat(header));
This adds the header back to the top of the numbered data array. - Clear and Set New Data in Destination Sheet:
destSheet.clear(); destSheet.getRange(1, 1, numberedData.length, numberedData[0].length).setValues(numberedData);
This clears theDestinationSheet
and sets the new data with the header, auto-incrementing ID, and filtered columns.
Conclusion
This corrected script ensures that all rows, including the second row, are processed and correctly added to the DestinationSheet
. By following the steps outlined in this blog post, you can efficiently manage your data in Google Sheets using Google Apps Script. Happy scripting!