从脚本将结果存储到Google Spreadsheet

时间:2014-11-05 19:55:48

标签: google-apps-script google-sheets

我有一个代码将数据输出到日志文件。如何使此脚本将输出发送到电子表格而不是日志,并且每当运行脚本时,在spreadhsheet中添加包含数据的附加工作表。应根据脚本运行的日期和时间命名工作表。

function listAllUsers() {
  var pageToken, page;
  do {
    page = AdminDirectory.Users.list({
      domain: 'mydomain.com',
      orderBy: 'givenName',

      pageToken: pageToken
    });
    var users = page.users;
    if (users) {
      for (var i = 0; i < users.length; i++) {
        var user = users[i];
        Logger.log('%s (%s)', user.name.fullName, user.primaryEmail);
      }
    } else {
      Logger.log('No users found.');
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
}

此致

2 个答案:

答案 0 :(得分:0)

这是一个更简单的实现,请参阅代码中的注释:

function listAllUsers() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.insertSheet(0);// create new sheet in first position
  sh.setName(Utilities.formatDate(new Date(),Session.getScriptTimeZone(),'MMM-dd-yyy@hh:mm:ss'));
  var data = [];// array to store values
  data.push(['user fullName', 'user primary Emai']);// store headers
  var pageToken, page;
  do {
    page = AdminDirectory.Users.list({
      domain: 'mydomain.com',
      orderBy: 'givenName',

      pageToken: pageToken
    });
    var users = page.users;
    if (users) {
      for (var i = 0; i < users.length; i++) {
        var user = users[i];
        Logger.log('%s (%s)', user.name.fullName, user.primaryEmail);
        data.push([user.name.fullName, user.primaryEmail]);//store in an array of arrays (one for each row)
      }
    } else {
      Logger.log('No users found.');
      data.push(['No users found.','--']);
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
  sh.getRange(1,1,data.length,data[0].length).setValues(data);
}

答案 1 :(得分:-1)

以下是如何将数据放入工作表的示例:

//now write this data to the Detail spreadsheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var destSheet = spreadsheet.getSheetByName("Detail");
var destSheet2 = spreadsheet.getSheetByName("Summary");

//Clear values before pasting array data
destSheet.getRange(2, 1, 300, 20).clear();
destSheet2.getRange(2, 1, 50, 20).clear();

var numFormat = "0.00%"

//set spreadsheet values and apply conditional formatting.

for (var i = 2; i < (arrEmpCapDet.length + 2); i++) {
  destSheet.getRange(i, 1, 1, arrEmpCapDet[i-2].length).setValues(new Array(arrEmpCapDet[i-2]));
  destSheet.getRange(i, 1, 1, arrEmpCapDet[i-2].length).setNumberFormat(numFormat);
} 

for (var i = 2; i < (arrEmpCapSum.length + 2); i++) {
  destSheet2.getRange(i, 1, 1, arrEmpCapSum[i-2].length).setValues(new Array(arrEmpCapSum[i-2]));
  destSheet2.getRange(i, 1, 1, arrEmpCapSum[i-2].length).setNumberFormat(numFormat);
  //for each item in the row, check if the value is above 100% or below 50%, then change color.
  for (var p = 2; p < 16; p++) {
    var tmpValue = destSheet2.getRange(i, p).getValue();
    //Logger.log(tmpValue);
    if (tmpValue > 1.0)  {
     destSheet2.getRange(i, p).setBackground('red');
    }
    if (tmpValue < 0.5)  {
     destSheet2.getRange(i, p).setBackground('green');
    }
  }
} 

查看here了解有关创建新工作表的信息。