Home > Enterprise >  How to code a loop for current date in apps script
How to code a loop for current date in apps script

Time:08-10

I'm trying to do a code in apps script, that finds the "today date" between all of these dates, and they will probably be updated with the time, what I currently have is this code:

 function requestSlack(method, endpoint, payload) {

const base_url = "https://slack.com/api/"

const headers = {

  'Authorization': "Bearer "   "MYTOKEN",

  'Content-Type': 'application/json'
}

var options = {

  headers: headers,

  method: method,

  payload: payload
}

let request_url;

  if (method == "POST") {

  request_url = base_url   endpoint

  options.payload = JSON.stringify(payload)

} else {

  request_url = base_url   endpoint


 }

const response = UrlFetchApp.fetch(request_url, options).getContentText();

const json = JSON.parse(response);

return {

  response_code: json.ok,

  response_data: json
}
}

function chamaMensagem() {



 let dados = getInformacoesPlanilha();

var payload = {

  "channel": "MY CHANNEL ID",

  "text": `Titulo da tarefa: ${dados.TituloTarefa[0]}`  

\nProprietario: ${dados.ProprietarioTarefa[0]}

\nDeadline: ${dados.DataConclusao[0]}

\nDescrição:${dados.DescricaoTarefa[0]},

 "charset": "application/json"
 
   }
  var response = requestSlack("POST", "chat.postMessage", payload);
}

function getInformacoesPlanilha(){

const ss = SpreadsheetApp.openByUrl("MY SPREADSHEET URL"); 

const sheet = ss.getSheetByName("MY SPREADSHEET NAME")

const TituloTarefa = sheet.getRange("C12:C18").getValues();

const ProprietarioTarefa = sheet.getRange("D12:D18").getValues();

const DataConclusao = sheet.getRange("F12:F18").getValues(); 

const DescricaoTarefa = sheet.getRange("G12:G18").getValues();  

 console.log(DataConclusao);

const dados = {

   TituloTarefa,ProprietarioTarefa,DataConclusao,DescricaoTarefa

}

var dataatual = new Date();

  var diasdataatual = Math.ceil(dataatual / (1000 * 3600 * 24));



 for(var i = 0; i < dados.length; i  ){

  var data = new Date(dados[i][DataConclusao]);

  var time = Math.abs(data.getTime());

  var dias = Math.ceil(time / (1000 * 3600 * 24));

var dif = parseInt(dias) - parseInt(diasdataatual);

Logger.log(dif);

    if(dif == 0){

      Logger.log("Mesma data")


  } else{

      Logger.log("ainda está no prazo");
  } 
  }
  return dados;
}

I realized that my code was incomplete and that made it difficult for you to help me, sorry guys. This is my complete code, I just delete some specific information, the error is in finding the current date in the last function

CodePudding user response:

Try something like this

function lfunko() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Dados");//the sheet where you have your data
  const dados = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
  const dt = new Date();
  const dtv = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate()).valueOf();//value at midnight
  dados.forEach((r, i) => {
    let d = new Date(r[DataConclusao]).valueOf();//assumes DataConclusao is an integer between 0 and r.length - 1 inclusive
    let dv = new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf();//value at midnight
    if(dv == dtv) {
      //these are rows that have the correct date
    }
  })
}
  • Related