简体   繁体   English

如何在google sheet app脚本中计算剩余时间

[英]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.下面是我的工作表单元格。

在此处输入图像描述

What I need:我需要的:

I need to sum up estimated working days which have 100%.我需要总结有 100% 的估计工作日 In the picture, I need to get 0.3.在图片中,我需要得到 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 .我的状态栏从D4开始,我的预计工作日从C4开始。

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 .我遍历状态列,对于每个包含值 '100%' 的单元格,我将其汇总为变量sm Then I paste the resulting value to cell D2 as your screenshot illustrates.然后我将结果值粘贴到单元格D2 ,如您的屏幕截图所示。 Feel free to change the sheet name, in my case Sheet1 .随意更改工作表名称,在我的情况下为Sheet1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM