Home‎ > ‎

scripting

// Contact philip.ngai@gmail.com for help running or modifying this script

function onOpen() {
  SpreadsheetApp.getUi()
      .createMenu('CreatePrintouts')
      .addItem('Run', 'walker')
      .addToUi();
}

function walker() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var range = sheet.getDataRange();
  var values = range.getValues();
  var currentCell;
  var headerArray=[];
  var numColumns = sheet.getLastColumn();
  var numRows = sheet.getLastRow();
  
  for (var row = 0; row < 1; row++) {
    for (var col = 0; col < numColumns; col++) {
      currentCell = values[row][col];
      headerArray.push(currentCell);
    }
  }

  var doc = DocumentApp.create('Survey Responses');
  var body = doc.getBody();
  var headerText;
  var thisPara;
  for (var row = 1; row < numRows; row++) {
    for (var col = 0; col < numColumns; col++) {

      headerText  = headerArray[col];
      thisPara = body.appendParagraph(headerText);
      thisPara.setBold(true);

      currentCell = values[row][col];
      thisPara = body.appendParagraph(currentCell + "\n");
      thisPara.setBold(false);
    }
    body.appendPageBreak();
  } 
}

Comments