2012-05-10 66 views
0

我希望能夠允許我的用戶爲他們正在創建的文檔指定字體大小(和族)。我有一個他們在配置頁面上選擇的各種大小的列表框,但我不確定如何將它分配給代碼。例如,這工作正常:傳遞一個變量作爲DocumentApp.Attribute.FONT_SIZ

'var lHeading2style = {}; lHeading2style [DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.headingStyle2font; lHeading2style [DocumentApp.Attribute.FONT_SIZE] = 14; lHeading2style [DocumentApp.Attribute.BOLD] = false;'

但是,如果我設置如下所示的變量,如何將其附加到我的代碼行?

var headingStyle3FontSize ='8';

var lHeading3style = {}; lHeading3style [DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily + headingStyle3font; lHeading3style [DocumentApp.Attribute.FONT_SIZE] ='headingStyle3FontSize'; lHeading3style [DocumentApp.Attribute.BOLD] = false; //格式樣式

我也試着 lHeading3style [DocumentApp.Attribute.FONT_SIZE] = headingStyle3FontSize;

感謝您的幫助!

回答

1

您應該爲變量使用數字值,而不是字符串值。

var headingStyle3FontSize = 8; 
lHeading3style[DocumentApp.Attribute.FONT_SIZE] = headingStyle3FontSize; 
+0

啊..謝謝!! 將'headingStyle3font'作爲變量的解決方案是什麼?我想讓他們從選擇列表中選擇它。 var lHeading3style = {}; lHeading3style [DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily + ** ** headingStyle3font; lHeading3style [DocumentApp.Attribute.FONT_SIZE] ='headingStyle3FontSize'; lHeading3style [DocumentApp.Attribute.BOLD] = false; //格式化樣式 –

+0

FONT_FAMILY屬性包含以下的枚舉值:https://developers.google.com/apps-script/class_documentapp_fontfamily。你不能追加一個字符串來創建這些枚舉值,你需要將你的字符串值轉換爲一個枚舉值。要做到這一點的一種方法是有一個開關塊:http://www.w3schools.com/js/js_switch.asp –

+0

好吧,所以這個想法是寫在我的代碼 'var lHeading3style = {}; lHeading3style [DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily + **航向gStyle3font **; lHeading3style [DocumentApp.Attribute.FONT_SIZE] = 'headingStyle3FontSize';' 然後只是測試使用我的選擇列表的值 '開關(字體) { 字體= 'ARIAL' 變種lHeading3style = {}; lHeading3style [DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily + ARIAL; lHeading3style ['DocumentApp.Attribute.FONT_SIZE] ='headingStyle3FontSize';' break; –

相關問題