Google电子表格
我在找一种编辑单元格内容的方法。我已经在电子表格服务文档上搜索过了,但是没有发现什么能帮助我完成我想要完成的事情。
我想做的是操纵一个单元格的内容。这方面的一个例子是:
任何帮助都是非常感谢的!谢谢!
发布于 2015-06-09 21:02:05
下面示例的每个部分都可以更简洁、高效、优雅和自适应地完成--这确实不是很好的代码--但我希望尽可能清楚地说明组件是如何工作的。
首先,这里有一个过度简化的函数,可以将小时转换为24小时格式。我不建议你使用这个,因为它只适用于按你写的格式格式化的小时时间,例如“下午3点”或“凌晨2点”-“下午2点30分”根本不起作用。要获得更复杂的时间转换,请查看以下答案:将12小时hh:mm AM/PM转换为24小时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"
}https://stackoverflow.com/questions/27971780
复制相似问题