khukho
khukho

Reputation: 476

How to calculate remaining time in google sheet app script

I need to calculate the time remaining in google sheets app script. Below is my work sheet cells.

enter image description here

What I need:

I need to sum up estimated working days which have 100%. In the picture, I need to get 0.3.

What I've tried:

var mainWsName = "master";

function myFunction() {  
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(mainWsName);
  var values = sheet.getRange('D4:D48').getValues();
  
  var a = values.join().split(',');
 
  Logger.log(a); // [, 1, 1, 1, , ]
}

My status column starts from D4 and my estimated working days starts from C4.

Upvotes: 1

Views: 290

Answers (1)

Marios
Marios

Reputation: 27350

Here is the solution you are looking for:

function myFunction() {
  
 const ss = SpreadsheetApp.getActive();
 const sh = ss.getSheetByName('Sheet1');
 const est_works = sh.getRange('C4:C48').getValues().flat([1]); 
 const status = sh.getRange('D4:D48').getDisplayValues().flat([1]);
  
  var sm = 0;
  status.forEach( (element,index) => {
  if(status[index]=== '100%'){sm+=est_works[index]}             
});

sh.getRange('D2').setValue(sm.toFixed(2))
  
}

Explanation:

I iterate through the status column and for every cell that contains the value '100%' I sum it up to the variable sm. Then I paste the resulting value to cell D2 as your screenshot illustrates. Feel free to change the sheet name, in my case Sheet1.

Upvotes: 1

Related Questions