簡體   English   中英

Google表單要求持續時間,以及如何在Google App腳本中進行計算

[英]Google Form asks for duration, how to calculate with this in Google App Script

我定期使用stackoverflow來找到問題的答案,但似乎無法解決這個問題。 所以這是我的第一個問題:

我有一個Google表格,其中(其中包括)要求提供工作時間。 我希望Google電子表格包含表單答案,並添加一些列。 在這種情況下,我想使用126小時的小時費率來增加工作成本,

但是我在計算持續時間時一直遇到問題:我的腳本要么告訴我它的文本(如果我在檢索數據時使用getDisplayValue),要么給我電子表格本身一個#NUM錯誤。

誰能指出我的解決方案,如何從表單字段中檢索小時和分鍾(時間作為持續時間),以便我可以在腳本中使用一些基本的數學方法?

我設置了一個小表格並連接了顯示我的問題的電子表格。 表單示例僅詢問持續時間,並將其放置在電子表格的第2列中。在電子表格中,我設置了一個在表單提交上運行的腳本,我嘗試解釋我所做的所有步驟。 該腳本應采用形式輸入,將其轉換為小時(作為數字字段),然后將其乘以PricePerHour。 結果應放在表單提交的同一行的第3列中。

到目前為止,這是我的腳本:

// CALCULATE COST OF JOB
function calculatePriceDuration(e) {

  // get source data
  var sourceSheet          = SpreadsheetApp.getActiveSheet();                                            // connect to source sheet
  var sourceRow            = sourceSheet.getActiveRange().getRow();                                      // connect to event row (form submit)


  // get destination data
  var destinationSheet    = sourceSheet;                                                                 // connect to destination sheet
  var destinationRow      = sourceRow;                                                                   // connect to destination row
  var destinationColID    = 3;                                                                           // set column number of value to paste


  // set variables
  var colID_FormDuration   = 2;                                                                          // set column number where the form places the duration
  var formDuration         = sourceSheet.getRange(sourceRow, colID_FormDuration).getDisplayValue();      // get value for duration 


  // set price per hour 
  var PricePerHour         = 126;

  // calculate job price
  var PriceForJob          = formDuration * PricePerHour;

  // set destination cell
  destinationSheet.getRange(destinationRow,destinationColID).setValue(PriceForJob);                      // paste value in the 3rd column of the same row of form submit

}

電子表格本身可以在這里找到:

表格可以在這里找到:

任何幫助深表感謝! 親切的問候,羅布

嘗試這個:

function calcTimeDifference(Start,End)
{
  if(Start && End)
  {
    var second=1000;
    var minute=60*second;
    var hour=minute*60;
    var day=hour*24;
    var t1=new Date(Start).valueOf();
    var t2=new Date(End).valueOf();
    var d=t2-t1;
    var days=Math.floor(d/day);
    var hours=Math.floor(d%day/hour);
    var minutes=Math.floor(d%day%hour/minute);
    var seconds=Math.floor(d%day%hour%minute/second);
    return 'dd:hh:mm:ss\n' + days + ':' + hours + ':' + minutes + ':' + seconds;  
  }
  else
  {
    return 'Invalid Inputs';
  }
}

感謝chirag90編輯我的問題,感謝tehhowch和cooper為我提供答案。 不幸的是,我的Java語言技能太差,無法真正理解您的答案。

幸運的是,我在StackOverflow上找到了這篇文章 ,是解決我的問題的完美方法:

function getValueAsSeconds(range) {
  var value = range.getValue();

  // Get the date value in the spreadsheet's timezone.
  var spreadsheetTimezone = range.getSheet().getParent().getSpreadsheetTimeZone();
  var dateString = Utilities.formatDate(value, spreadsheetTimezone, 
      'EEE, d MMM yyyy HH:mm:ss');
  var date = new Date(dateString);

  // Initialize the date of the epoch.
  var epoch = new Date('Dec 30, 1899 00:00:00');

  // Calculate the number of milliseconds between the epoch and the value.
  var diff = date.getTime() - epoch.getTime();

  // Convert the milliseconds to seconds and return.
  return Math.round(diff / 1000);
}

function getValueAsMinutes(range) {
  return getValueAsSeconds(range) / 60;
}

function getValueAsHours(range) {
  return getValueAsMinutes(range) / 60;
}

再次感謝您的努力,也許您可​​以指導我參加(基於在線/荷蘭語的)課程,在這里我可以學習如何正確使用Javascript。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM