英文:
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('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"; // Desired duration format (hh:mm)
// Format duration in the range fillDownRange
fillDownRange.setNumberFormat(durationFormat);
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论