Google Script: Conditionally copy rows from one spreadsheet to another

前端 未结 3 1915
死守一世寂寞
死守一世寂寞 2021-01-14 21:17

I\'ve found the basic solution https://stackoverflow.com/a/4809413/1438650 but it lacks both the conditional and non-contiguous aspects of my project. I need the the copy to

相关标签:
3条回答
  • 2021-01-14 21:22

    Here are some tips for you:

    • Get a range that includes the entire row, and then call getValues() to get all the values at once, instead of one at a time like you are doing now.
    • Wrap an if() statement around your appendRow() call, using the condition you want to test for.
    • It's OK to have you appendRow(), since there is no method to append multiple rows at once.
    0 讨论(0)
  • 2021-01-14 21:25

    Here is a script "proposal" you could complete to make it work. The 'if' statement needs to be implemented, I couldn't do it without knowing exactly what you needed. The general idea of using arrays it there, I'm sure you'll manage to 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);
    } 
    

    remark : data[i] is a row of your source sheet, data[i][j] is a cell in this row, from there you can adress any cell in any row. One usually do that in a for loop but you could do it without looping in each row to get the data you want.

    Edit#1: your condition to copy non contiguous data could be something like this :

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

    sorry for being a bit long. Edit#2 : looking at your target doc I noticed that you have multiple sheets in it, you should also tell the script on which sheet it should write data. Here is an example of how to do it :

      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 
    
    0 讨论(0)
  • 2021-01-14 21:27

    Blockquote

    This is my Coding, I think this would help you.

    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++;
          }
        }
      }
    
    0 讨论(0)
提交回复
热议问题