2014-05-03 59 views

回答

0

一些實驗和這個作品

var final = "proper title please"; 
var tempindex = 0; 
var tempchar = new Array(); 
function capitalize() 
{ 
tempchar=final.split(""); 
tempchar[tempindex]=tempchar[tempindex].toUpperCase(); 
final=tempchar.join(""); 
tempindex = final.indexOf(" ", tempindex)+1; 
if (final.search(/ [a-z]/) != -1) { 
capitalize(); 
} else { 
return final; 
} 
} 
capitalize(); 
0

這是我在Google Docs幫助發現:

function onOpen() { 
var ui = SpreadsheetApp.getUi(); 
ui.createMenu('Custom Menu') 
    .addItem('Capitalize', 'proper') 
    .addToUi(); 
} 


function proper() { 
    var arr = [], 
     s = SpreadsheetApp.getActiveSheet(); 
     s.getRange('A2:A') 
      .getValues() 
      .forEach(function (r) { 
       if (r[0]) arr.push([toTitleCase(r[0])]) 
      }); 
    s.getRange(2, 2, arr.length, arr[0].length) 
     .setValues(arr); 
} 

function toTitleCase(str) { 
    return str.replace(/\w\S*/g, function (txt) { 
     return txt.charAt(0) 
      .toUpperCase() + txt.substr(1) 
      .toLowerCase(); 
    }); 
} 
相關問題