1

Google 시트를 구문 분석하고 문서에서 셀을 멋지게 형식화하는 스크립트를 작성하고 있습니다. 열 1의 셀 데이터를 항상 굵게 표시하고 열 6의 셀 데이터를 항상 기울임 체로 표시하고 싶습니다. 문제는 단락을 문서 본문에 추가 한 후에 특성 변경이 전체 문서에 적용된다는 것입니다. 문서 본문에 셀 데이터를 추가하기 전에 셀 데이터를 굵게/기울임 꼴로 표시하는 방법이 있습니까?Google 문서 도구 스크립트에서 한 줄을 어떻게 굵게 표시합니까?

function readRows() { 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    var rows = sheet.getDataRange(); 
    var numRows = rows.getNumRows(); 
    var numCols = rows.getNumColumns(); 
    var values = rows.getValues(); 

    var doc = DocumentApp.create("Smogon Formatted"); 
    var docBody = doc.getBody(); 

    for (var i = 2; i <= numRows; i++) { 

    for (var j = 1; j <= numCols; j++){ 

     var cellData = rows.getCell(i, j).getValue() 

     // Format data based on column 
     if (j == 1) { 
      docBody.appendParagraph(cellData).editAsText().setBold(true); 
     } else if (j == 2 || j == 3) { 
      var imgFormula = rows.getCell(i, j).getFormula(); 
      var imgUrl = getImageUrl(imgFormula); 
      docBody.appendParagraph("[img]" + imgUrl + "[/img]"); 
     } else if (j == 6) { 
      docBody.appendParagraph(cellData).editAsText().setItalic(true); 
     } else { 
      docBody.appendParagraph(cellData); 
     } 
    } 
    } 
}; 

편집 : 당신이 스타일은 매우 쉽게 모든 단락에 스타일을 할당 할 수 속성을 사용하는 경우 setAttributes 방법

function readRows() { 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    var rows = sheet.getDataRange(); 
    var numRows = rows.getNumRows(); 
    var numCols = rows.getNumColumns(); 
    var values = rows.getValues(); 

    var doc = DocumentApp.create("Smogon Formatted"); 
    var docBody = doc.getBody(); 

    for (var i = 2; i <= numRows; i++) { 

    for (var j = 1; j <= numCols; j++){ 

     var cellData = rows.getCell(i, j).getValue() 

     // Format data based on column 
     if (j == 1) { 
      docBody.appendParagraph(cellData).setAttributes(style1); 
     } else if (j == 2 || j == 3) { 
      var imgFormula = rows.getCell(i, j).getFormula(); 
      var imgUrl = getImageUrl(imgFormula); 
      docBody.appendParagraph("[img]" + imgUrl + "[/img]"); 
     } else if (j == 6) { 
      docBody.appendParagraph(cellData).setAttributes(style2); 
     } else { 
      docBody.appendParagraph(cellData); 
     } 
    } 
    } 
}; 

// Style definitions as global variables 
var style1= {}; 
style1[DocumentApp.Attribute.BOLD] = true; 
var style2= {}; 
style2[DocumentApp.Attribute.ITALIC] = true; 

답변

1

를 사용하여, # 2를보십시오, 당신은 어떤 문서 요소에 대해 그것을 실제로 할 수 있습니다. 여기

..

어떻게 작동하는지 보여주기 위해 기본 예제 코드 :
( doc here)

function exportToDoc(){ 
    var doc = DocumentApp.openById('16i----L53WTDpzuLyhqQQ_E');// or create a new doc (but not while you test it :-) 
    var body = doc.getBody(); 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    var values = sheet.getDataRange().getValues(); 
    for (var i in values){ 
    var rowData = values[i].join(' + '); 
    if (i == 1) { 
     body.appendParagraph(rowData).setAttributes(style2); 
    } else if (i == 2) { 
     body.appendParagraph(rowData).setAttributes(style1) 
    } 
    } 
    doc.saveAndClose(); 
} 

// Style definitions as global variables 
    var style1 = {};// style example 1 
    style1[DocumentApp.Attribute.FONT_SIZE] = 10; 
    style1[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.CONSOLAS; 
    style1[DocumentApp.Attribute.FOREGROUND_COLOR] = "#444400"; 
    var style2 = {};// style example 2 
    style2[DocumentApp.Attribute.FONT_SIZE] = 16; 
    style2[DocumentApp.Attribute.FONT_FAMILY] =DocumentApp.FontFamily.ARIAL_NARROW; 
    style2[DocumentApp.Attribute.FOREGROUND_COLOR] = "#005500"; 
// 

예를 들어 임의의 데이터가 발생할 : 나는 반복의 당신의 방법을 적용 할 때 난 여전히 같은 결과를 얻고있다

enter image description here

+0

합니다. 아마도 제 스크립트가 그대로 작동 할 수 있다고 제안 할 수 있습니까? – Shwheelz