2015-01-15 9 views
0

Google App 스크립트 스프레드 시트Google App 스크립트 스프레드 시트

셀의 내용을 편집하는 방법을 찾고 있습니다. 나는 Spreadsheet Service docs을 통해 수색하고 나가 달성하고 싶었던 것을 위해 저를 도울 많은 것을 찾아 내지 않았다.

내가하고 싶은 것은 셀의 내용을 조작하는 것입니다. 이것의 예는 다음과 같습니다

  • 셀은 A1은 "오후 1시에서 오후 5시까지"를 포함
  • 나는 두 개의 세포로 A1을 분할 할 B1은 "오후 1시"로 말하고 C1은 "오후 5시"로
  • 그런 다음 B1과 C1 셀을 군사 시간으로 변경하려고합니다. 그래서 결국 B1은 13:00이고 C1은 17:00가 될 것입니다.)

도움을 주시면 감사하겠습니다! 감사!

답변

1

아래 예제의 모든 부분은보다 간결하고, 효율적이며, 우아하고 적응력있게 수행 할 수 있습니다. 실제로는 좋은 코드는 아닙니다.하지만 구성 요소를 가능한 한 명확하게 만들어서 어떻게 작동하는지 볼 수 있기를 바랍니다.

먼저 시간을 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" 
} 
관련 문제