Google Apps Script列格式

huangapple go评论78阅读模式
英文:

Google Apps Script column format

问题

I just set up a script to automatically calculate the duration of shifts as they're submitted from a Google Form and transferred to a linked Google Sheet. The duration keeps formatting as hh:mm:ss AM/PM and I want it to format up as hh:mm (no AM/PM) without messing up the current code or having to constantly change it via the number format tab in Google Sheets. Help?

Current code is:

function FillFormulas() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Responses');
  var lastRow = spreadsheet.getLastRow();
  spreadsheet.getRange("F2").setFormula("=E2-D2");
  var fillDownRange = spreadsheet.getRange(2, 6, (lastRow - 1)); 
  spreadsheet.getRange("F2").copyTo(fillDownRange);
}

I tried multiple different things, don't quite remember what, but none of them worked. The code kept getting messed up.

英文:

I just set up a script to automatically calculate the duration of shifts as they're submitted from a Google Form and transferred to a linked Google Sheet. The duration keeps formatting as hh:mm:ss AM/PM and I want it to format up as hh:mm (no AM/PM) without messing up the current code or having to constantly change it via the number format tab in Google Sheets. Help?

Current code is:

function FillFormulas() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Responses');
  var lastRow = spreadsheet.getLastRow();
  spreadsheet.getRange("F2").setFormula("=E2-D2");
  var fillDownRange = spreadsheet.getRange(2, 6, (lastRow - 1)); 
  spreadsheet.getRange("F2").copyTo(fillDownRange);
}

I tried multiple different things, don't quite remember what, but none of them worked. The code kept getting messed up.

答案1

得分: 0

虽然我不确定我是否能正确理解您期望的结果,但以下修改如何?

从:

var fillDownRange = spreadsheet.getRange(2, 6, (lastRow - 1));

至:

var fillDownRange = spreadsheet.getRange(2, 6, (lastRow - 1)).setNumberFormat('hh:mm'); // 或 'hh":"mm'

参考:

英文:

Although I'm not sure whether I could correctly understand your expected result, how about the following modification?

From:

var fillDownRange = spreadsheet.getRange(2, 6, (lastRow - 1)); 

To:

var fillDownRange = spreadsheet.getRange(2, 6, (lastRow - 1)).setNumberFormat('hh:mm'); // or 'hh":"mm'

Reference:

答案2

得分: 0

以下是您要翻译的部分:

希望这是您想要的,对您有效。

function FillFormulas() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Responses');
  var lastRow = spreadsheet.getLastRow();
  spreadsheet.getRange("F2").setFormula("=E2-D2");
  var fillDownRange = spreadsheet.getRange(2, 6, (lastRow - 1)); 
  spreadsheet.getRange("F2").copyTo(fillDownRange);

  var durationFormat = "hh:mm"; // 所需的持续时间格式(hh:mm)

  // 在 fillDownRange 范围内格式化持续时间
  fillDownRange.setNumberFormat(durationFormat);
}
英文:

I hope this is what you want and it works for you.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

function FillFormulas() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(&#39;Responses&#39;);
  var lastRow = spreadsheet.getLastRow();
  spreadsheet.getRange(&quot;F2&quot;).setFormula(&quot;=E2-D2&quot;);
  var fillDownRange = spreadsheet.getRange(2, 6, (lastRow - 1)); 
  spreadsheet.getRange(&quot;F2&quot;).copyTo(fillDownRange);

  var durationFormat = &quot;hh:mm&quot;; // Desired duration format (hh:mm)

  // Format duration in the range fillDownRange
  fillDownRange.setNumberFormat(durationFormat);
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月11日 07:32:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定