简体   繁体   中英

Copy a row to new sheet based on value in a cell

I'm trying to create a Google Script that copies rows from one Google Sheet into different sheets based on the value of a cell.

The cell value are states in the United States. The master spreadsheet has data from a registration form that is being imported into it all the time. When a new registration happens (and the data is imported into the master sheet), I'd like the script to run and copy that row of data into the appropriate state sheet.

Here's where I'm at:

  1. Get master sheet
  2. Find the last row
  3. Get the value of the cell in the column "state"
  4. Copy that row into one of 50 different sheets depending on what state it is.
  5. Run the script every time the master sheet is updated (via an API).

Any help would be appreciated. I'm definitely a newbie when it comes to scripting and this is just hurting my head.

Here's the code I have so far:

function myFunction() {

// Get Source Spreadsheet
var source = SpreadsheetApp.getActiveSpreadsheet();

// Get Source Sheet from Spreadsheet
var source_sheet = source.getActiveSheet();

// Get Active Range from Sheet
var lastRow = sheet.getLastRow();

// Get the Value of the State Cell
var cellValue = Range.getCell(lastrow,3);

// Copy Last Row to Appropriate State Sheet
if ( cellValue == 'Alaska') {
    var target = SpreadsheetApp.openById("");
    var target_sheet = target.getSheetByName("Sheet1");
    target_sheet.appendRow(lastRow);
}
}

With this code:

// Get Active Range from Sheet
var lastRow = sheet.getLastRow();

The getLastRow() method returns an integer. Then further down in your code, you are using the lastRow variable as the data for appendrow() which won't work;

target_sheet.appendRow(lastRow);

The code should probably be something like this:

var target = SpreadsheetApp.openById("");
var target_sheet = target.getSheetByName("Sheet1");

var lastRowOfData = source_sheet
  .getRange(source_sheet.getLastRow(), 1, 1, source_sheet.getLastColumn())
  .getValues();  //Returns a two dimensional array

var oneD_Array = lastRowOfData.join().split(","); //Creates a one D array
target_sheet.appendRow(oneD_Array);

The appendRow() method takes a one dimensional array. The getValues() method returns a 2 Dimensional array, so it must be converted. Because you are only getting one row of data, it's easy to convert. The outer array only has one inner array. The one inner array has all the cell values of the row.

Here's the answer for what I came up with for the code:

function myFunction() {

// Get Source Spreadsheet
var source = SpreadsheetApp.getActiveSpreadsheet();

// Get Source Sheet from Spreadsheet
var source_sheet = source.getActiveSheet();

// Get Last Row
var lastRow = source_sheet.getLastRow();

// Get Last Column
var lastColumn = source_sheet.getLastColumn();

// Get Last Row of Data  
var lastRowOfData = source_sheet.getRange(lastRow, 1, 1, lastColumn).getValues();

// Creates a one dimensional array  
var oneD_array = lastRowOfData.join().split(",");

// Get the Value of the State Cell
var cellValue = source_sheet.getRange(2,7).getValue();


// Copy Last Row to Appropriate State Sheet
if ( cellValue == "New York" ) {
    var target = SpreadsheetApp.openById("");
    var target_sheet = target.getSheetByName("Sheet1");
    target_sheet.appendRow(oneD_array);
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM