Google Script: Conditionally copy rows from one table to another

I found a basic solution to https://stackoverflow.com/a/3/94961/ ... but it lacks both the conditional and non-contiguous aspects of my project. I need a copy based on the Col G value, and then copy the columns A, B and H: S to another table.

function movePros() {
  var date = Utilities.formatDate(new Date, "CST", "yyyy-MM-dd'T'HH:mm:ss'Z'")            //"MM/dd/yyyy"
  var source = SpreadsheetApp.getActiveSpreadsheet();
  var target = SpreadsheetApp.openById("0ApBdJlRcFy9XdGx0aXRabWlweGxyY3czZzZIemVla3c");
  var lastRow = source.getLastRow();
  var sheet = source.getSheetByName("City");
 var data = sheet.getDataRange.getValues();  //This makes an array, right?
   Browser.msgBox(data.slice(1))
  // I've tried, but still don't know how get at the individual cell values so that I can both check them and write them in the correct order which is different than the source
  var prospect=[DateCalled,PriceSurvey,VolSurvey,Company,Phone,Contact,OdmNotes,Address,City,State,Zip,County,Sic,"","","","","","","","=IFERROR(IF(INDIRECT(\"W\"&ROW())<>\"\",TEXT(INDIRECT(\"W\"&ROW()),\"yyyyww\")+0,IF(INDIRECT(\"T\"&ROW())<>\"\",TEXT(INDIRECT(\"T\"&ROW()),\"yyyyww\")+0,IF(INDIRECT(\"A\"&ROW())<>\"\",TEXT(INDIRECT(\"A\"&ROW()),\"yyyyww\")+1,\"\"))),\"\")","U","","","","","",ODM,"",""];  
// var prospect=[data()[0]..... ugh, I don/'t know....  
 /*    
var ODM = sheet.getRange("A"+i).getValues();
var DateCalled = sheet.getRange("B"+i).getValues();
var PriceSurvey = sheet.getRange("H"+i).getValues();
var VolSurvey = sheet.getRange("I"+i).getValues();
var Company = sheet.getRange("J"+i).getValues();
var Phone = sheet.getRange("K1"+i).getValues();
var Contact = sheet.getRange("L"+i).getValues();
var OdmNotes = sheet.getRange("M"+i).getValues();
var Address = sheet.getRange("N"+i).getValues();
var City = sheet.getRange("O"+i).getValues();
var State = sheet.getRange("P"+i).getValues();
var Zip = sheet.getRange("Q"+i).getValues();
var County = sheet.getRange("R"+i).getValues();
var Sic = sheet.getRange("S"+i).getValues(); 
 */ 

while (i <= lastRow) {
  var i = 2 
  if (sheet.getRange("G"+i).getValue()==1 && sheet.getRange("U"+i).getValue()=="") {  //cheking 

target.appendRow(prospect); 

source.getRange("U"+i).setValue(date);
i++;

} else {
   i++;
  }     
  } 


}
+2
source share
3 answers

Here is a script suggestion that you can fill out to make it work. The operation "if" must be implemented, I could not do this without knowing exactly what you need. The general idea of ​​using arrays there, I'm sure you can make it work.

function movePros() {
  var date = Utilities.formatDate(new Date, "CST", "yyyy-MM-dd'T'HH:mm:ss'Z'")            //"MM/dd/yyyy"
  var source = SpreadsheetApp.getActiveSpreadsheet();
  var target = SpreadsheetApp.openById("0ApBdJlRcFy9XdGx0aXRabWlweGxyY3czZzZIemVla3c");
  var lastRow = source.getLastRow();
  var sheet = source.getSheetByName("City");
  var data = sheet.getDataRange().getValues();
  var prospect=['ODM','DateCalled','PriceSurvey','VolSurvey','Company','Phone','Contact','OdmNotes','Address','City,State','Zip','County','Sic']
  var line = new Array();
  var targetArray= new Array();
  targetArray.push(prospect);// create a header on first row of target
  for(i=0;i<data.length;++i){ // iterate rows
  for (j=0;j<data[i].length;++j){ // iterate columns
  if (place here your conditions if data must be copied or not, you can have many conditions alltogether){line.push(data[i][j]}// if cell meets condition, add to destination row
  }
  targetArray.push(line)// add row to destination (you could need to add a condition here too)
  }
target.getRange(1,1,targetArray.length,targetArray[0].length).setValues(targetArray);
} 

: data [i] - , [i] [j] - , . for, , .

# 1: :

  if (j==5||j==7||j==9){line.push(data[i][j])}

, . # 2: , , , script, . , :

  var ss = SpreadsheetApp.openById("your spreadsheet ID");// this is already done in your script
  SpreadsheetApp.setActiveSpreadsheet(ss);
  var sh = SpreadsheetApp.setActiveSheet(ss.getSheets()[0]);// select first sheet in target spreadsheet 
+2

Blockquote

, , .

function GetData() {
  var source = SpreadsheetApp.getActiveSpreadsheet();
  var target = SpreadsheetApp.openById("0Aj7gIt4f65xidEN1R1lxZksxeFRBYkdHWmVtdE5aOGc");
  var lastRow = source.getLastRow();
  var source_sheet = source.getSheetByName("Source");
  var target_sheet = target.getSheetByName("Target");

  var source_range = source_sheet.getDataRange();
  var target_range = target_sheet.getDataRange();

  var i = 2;
  while (i <= lastRow) {
  if (source_sheet.getRange("D"+i).getValue() == "Over Due" ) {
    var ItemNo = source_sheet.getRange("A"+i).getValue();
    var UserName = source_sheet.getRange("B"+i).getValue();
    var Location = source_sheet.getRange("C"+i).getValue();
    var Status = source_sheet.getRange("D"+i).getValue();
    var Comments = source_sheet.getRange("E"+i).getValue();
    var data = [ItemNo,UserName,Location,Status,Comments];
    target_sheet.appendRow(data);
    i++;
    } else {
      i++;
      }
    }
  }
+1

:

  • Get a range that includes the entire string, and then call getValues()to get all the values ​​at once, and not one at a time, as it is now.
  • Wrap the statement if()around your call appendRow()using the condition you want to test.
  • This is normal if you are appendRow(), as there is no method to add multiple lines at the same time.
0
source

All Articles