아래 예제의 모든 부분은보다 간결하고, 효율적이며, 우아하고 적응력있게 수행 할 수 있습니다. 실제로는 좋은 코드는 아닙니다.하지만 구성 요소를 가능한 한 명확하게 만들어서 어떻게 작동하는지 볼 수 있기를 바랍니다.
먼저 시간을 24 시간 형식으로 변환하는 간소화 된 함수를 사용합니다. 시간이 흘렀을 때 정확히 쓴 것처럼 형식화되어 있기 때문에 정확히 사용하지 않는 것이 좋습니다. 예 : "3PM"또는 "2AM"- "2:30 pm"은 전혀 작동하지 않습니다. 보다 정교한 시간 변환의 경우,이 대답 체크 아웃 : convert 12-hour hh:mm AM/PM to 24-hour hh:mm
function oversimplified_time_format_converter(input){
if(input.indexOf("PM")!==-1){
var hour = 12 + (input.substring(0,input.indexOf("PM"))*1)
if(hour>24){hour-=24}
} else {
var hour = input.substring(0,input.indexOf("AM"))
}
return hour
}
다음, 여기에 위의 과도하게 단순화 시간 형식 변환 함수를 사용 당신이 언급 한 작업을 수행하는 기능입니다.
function yourFunction() {
//First, locate your data:
var sheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheet/yourspreadsheetURL").getSheetByName("YourSheetName")
var cell = sheet.getRange("A1")
var cell_contents = cell.getValue() //The cell contents will be "1PM -5PM"
//Next, locate the cells where the results will go
var first_cell_to_the_right = cell.offset(0,1) //This is cell B1
var second_cell_to_the_right = cell.offset(0,2) //This is cell C1
//Next, get the text, split it into separate strings
var first_part = cell_contents.substring(0,cell_contents.indexOf(" - ")) //This will be "1PM"
var second_part = cell_contents.substring(cell_contents.indexOf(" - ")+3) //This will be "5PM"
//Now convert to 24-hour time format:
first_part = oversimplified_time_format_converter(first_part)
second_part = oversimplified_time_format_converter(second_part)
//Now write the results to your spreadsheet
first_cell_to_the_right.setValue(first_part) //Set value of B1 to "1PM"
second_cell_to_the_right.setValue(second_part) //Set value of C1 to "5PM"
}