0
votes

I'm creating a small database inside a sheet, I need the script to copy data to another sheet tab and I'm getting an empty error. I'm not expert on javascript so what code I'm missing here?

Basically, when you press a button you get a text modal with an input so the person writes the name and then the script gets all the Rows with a TRUE (checkbox) and copies everything to another sheet with a header saying the data and time with the name of the person. If returns nulled with everything FALSE wont copy and shows a text modal saying that there's no task done today.

Thanks in advance

function moveValuesOnly() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var source = ss.getRange ("RESUMO!A4:J99");
  var destSheet = ss.getSheetByName("LOG Resumo");
  var row = destSheet.getLastRow()+2; //the starting column of the range
  var column = 1; //the starting row of the range
  var numRows = 97; //the number of rows to return
  var numColumns = 10; //the number of columns to return
  var destRange = destSheet.getRange(row, column, numRows, numColumns);
  var input_text = Browser.inputBox("Encarregado de Turno","Escreve seu nome:", Browser.Buttons.OK);
  var now = new Date();
  var active = ss.getSheetByName("RESUMO");
  var condition = active.getRange('RESUMO!J4:J99').getValue();
  var valueToWatch = "TRUE";
  if (condition == valueToWatch) {
  destSheet.getRange(row-1,1,1,10).mergeAcross().setBackgroundRGB(224, 102, 102).setFontColor("white");
  destSheet.getRange(row-1,1,1).setValue(now + "  ~~   ENCARREGADO DE FECHAR TURNO: " + input_text).activate();
    source.copyTo(destRange, {contentsOnly: true}).setFontColor("black");
  } else {
  Browser.msgBox("Erro","Não exite tarefas completas hoje", Browser.Buttons.OK);
  }
}
1
This var condition = active.getRange('RESUMO!J4:J99').getValue(); is equivalent to this var condition = active.getRange('RESUMO!J4').getValue(); - Cooper

1 Answers

0
votes

There are following issue with your code:

  1. If you have TRUE and FALSE values as cell contents, Spreadsheets will automatically identify them as booleans, rather than strings. Thus, also valueToWatch needs to be a boolean rather than a string: var valueToWatch = true;
  2. If you want to verify the "TRUE" condition for each row - you need to do it in a loop. Consequently, you code needs to be modified a bit. One way to do so is to push all rows where the cell content in column J is TRUE into an array and then pass the contents of this array to a destination range with the same dimension as the array. This is what the resulting code would look like:
function moveValuesOnly() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var source = ss.getRange ("RESUMO!A4:J99");
  var destSheet = ss.getSheetByName("LOG Resumo");
  var row = destSheet.getLastRow()+2; //the starting column of the range
  var column = 1; //the starting row of the range
  var input_text = Browser.inputBox("Encarregado de Turno","Escreve seu nome:", Browser.Buttons.OK);
  var now = new Date();
  var active = ss.getSheetByName("RESUMO");
  var condition = active.getRange('RESUMO!J4:J99').getValues();
  destSheet.getRange(row-1,1,1,10).mergeAcross().setBackgroundRGB(224, 102, 102).setFontColor("white");
  destSheet.getRange(row-1,1,1).setValue(now + "  ~~   ENCARREGADO DE FECHAR TURNO: " + input_text).activate();
  var valueToWatch = true;
  var values=source.getValues();
  var array=[];
  for(var i=0;i<condition.length;i++){
   if (condition[i][0] == valueToWatch) {
    array.push( values[i]);    
   }
  }   
  if(!array) {
    Browser.msgBox("Erro","Não exite tarefas completas hoje", Browser.Buttons.OK);
  }
  else {
      var numRows = array.length; //the number of rows to return
      var numColumns = array[0].length; //the number of columns to return
      var destRange = destSheet.getRange(row, column,numRows , numColumns);
      destRange.setValues(array).setFontColor("black");
    }  
}