简体   繁体   中英

data transfert based on cell value on google sheet

I'm not good with coding and i tried to mix some codes that i found and made what i needed, but now i face a difficult problem for me, so I'm trying when to press the button that i created to run the script to send the data that have for example an 'X' in the last column. i successfully made the sending from one sheet to another but it send all the data for now i want to be sent only the chosen ones with an 'X'. any help I'll be grateful. this is my code thank you

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ss.getSheetByName("Sheet1");
  var url = "https://docs.google.com/spreadsheets/d/15nIAXcP0a14OvvBr5lww4tO23stQD4PEu0QDAcgaFyE/edit#gid=0";
  var ss2 = SpreadsheetApp.openByUrl(url);
  var pasteSheet = ss2.getSheetByName("Sheet2");

    // get source range
  var max = copySheet.getMaxRows().toString();
  var range = copySheet.getRange(2,1,max,4);
  var dataValues = range.getValues();
  
  for(var i = 1; i < dataValues.length; i++)
  {
    if(dataValues[i][5] === 'X')
    {
      copySheet.appendRow([dataValues[i][0], 
                          dataValues[i][1], 
                          dataValues[i][2], 
                          dataValues[i][3], 
                          dataValues[i][4]]);
    }
  }

  for(var i = 1; i < dataValues.length; i++)
  {
    if(dataValues[i][5] === 'X')
    {
      var clearRow = i+1;
      dataSheet.getRange('A' + clearRow + ':F' + clearRow).clear();
    }
  }

  // get destination range
  var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,max,1);

  // clear source values 
  Browser.msgBox('Commande Confirmer');
}

This is all that your function does

dataSheet is undefined so I assume it was csh

function lfunko() {
  const ss = SpreadsheetApp.getActive();
  const csh = ss.getSheetByName("Sheet1");
  const ss2 = SpreadsheetApp.openByUrl("ssurl");
  const psh = ss2.getSheetByName("Sheet2");
  var vs = csh.getRange(2, 1, csh.getLastRow() - 1, csh.getLastColumn()).getValues();
  vs.forEach((r,i) => {
    if (vs[i][5] === 'X') {
      csh.appendRow([vs[i][0], vs[i][1], vs[i][2], vs[i][3], vs[i][4]]);
      csh.getRange(i + 1, 1, 1, 6).clear();
    }
  });
}

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