Google Apps脚本:选择电子表格中的单个列

时间:2015-08-19 20:34:21

标签: google-apps-script google-sheets google-docs

我在下面有一个函数可以将数据从google工作表打印到google doc。有没有办法改变它,所以它只打印出来自列'1'(或列'B')的数据?

function saveAsDOC() {
  var fileName = "Announcements Test File";
  var doc = DocumentApp.create(fileName);
  SpreadsheetApp.getActiveSpreadsheet().toast('Generating file..', 'Status', 3);

  // Get the range in the spreadsheet
  var ws = SpreadsheetApp.getActiveSpreadsheet().getDataRange();
  try {
    var data = ws.getValues();

    // Announcements loop
    if (data.length > 1) { // Make sure data is longer than one character
      for (var row = 1; row < data.length; row++) {
        if(new Date(data[row][2]) <= new Date()) { // Check the date
          for (var col = 1; col < data[row].length; col++) {
            if(data[row][col]!=""){ // Make sure the data is not empty
              doc.appendParagraph(data[row][col]); // Append announcement
              doc.appendParagraph("") // Line break between announcements
            }
          }
        }
      }
    }
  }

  catch(err) {
    Logger.log(err);
    Browser.msgBox(err); // show the error messages in the browser
  }
}

1 个答案:

答案 0 :(得分:0)

尝试将col变量从变量更改为常量值:

变化:

[col]

为:

[2]

看看会发生什么。