将Google Apps脚本结果导出到Google表格

时间:2019-07-04 13:25:56

标签: javascript google-apps-script google-sheets gsuite

我有这个脚本,它将当前使用的许可证数量提取到日志文件中。我们希望每天运行脚本以建立一种使用模式。 我无法弄清楚如何将脚本结果保存/导出到Google表格。有人可以帮忙吗?

function getNumberOfLicenses() {
  var tryDate = new Date();
  var dateString = tryDate.getFullYear().toString() + "-" + (tryDate.getMonth() + 1).toString() + "-" + tryDate.getDate().toString();
  while (true) {
    try {
      var response = AdminReports.CustomerUsageReports.get(dateString,{parameters : "accounts:gsuite_unlimited_total_licenses,accounts:gsuite_unlimited_used_licenses"});
      break;
    } catch(e) {
      //Logger.log(e.warnings.toString());
      tryDate.setDate(tryDate.getDate()-1);
      dateString = tryDate.getFullYear().toString() + "-" + (tryDate.getMonth() + 1).toString() + "-" + tryDate.getDate().toString();
      continue;
    }
  };
  var availLicenseCount = response.usageReports[0].parameters[0].intValue;
  var usedLicenseCount = response.usageReports[0].parameters[1].intValue;
  Logger.log("Available licenses:" + availLicenseCount.toString());
  Logger.log("Used licenses:" + usedLicenseCount.toString());
  return availLicenseCount;
}

1 个答案:

答案 0 :(得分:0)

尝试一下:

function getNumberOfLicenses() {

  var ss = SpreadsheetApp.openById("SHEET_ID"); // Sheet where you want to write the result.

  var tryDate = new Date();
  var dateString = tryDate.getFullYear().toString() + "-" + (tryDate.getMonth() + 1).toString() + "-" + tryDate.getDate().toString();
  while (true) {
    try {
      var response = AdminReports.CustomerUsageReports.get(dateString,{parameters : "accounts:gsuite_unlimited_total_licenses,accounts:gsuite_unlimited_used_licenses"});
      break;
    } catch(e) {
      //Logger.log(e.warnings.toString());
      tryDate.setDate(tryDate.getDate()-1);
      dateString = tryDate.getFullYear().toString() + "-" + (tryDate.getMonth() + 1).toString() + "-" + tryDate.getDate().toString();
      continue;
    }
  };
  var availLicenseCount = response.usageReports[0].parameters[0].intValue;
  var usedLicenseCount = response.usageReports[0].parameters[1].intValue;
  Logger.log("Available licenses:" + availLicenseCount.toString());
  Logger.log("Used licenses:" + usedLicenseCount.toString());

  ss.appendRow([availLicenseCount.toString(), usedLicenseCount.toString()]) // Adding a row with the data in the first two columns of the sheet.

  return availLicenseCount;
}

此代码将打开您想要的工作表(您将需要它的ID),并将在代码末尾添加数据。这两个变量将添加到下一个空行的前两个单元格中。 Here您将找到有关如何使用SpreadsheetApp的文档和指南。