簡體   English   中英

將Google Apps腳本結果導出到Google表格

[英]Export Google Apps Script result to Google Sheet

我有此腳本可將當前使用的許可證數量提取到日志文件中。 我們希望每天運行腳本以建立一種使用模式。 我無法弄清楚如何將腳本結果保存/導出到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;
}

嘗試這個:

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),並將在代碼末尾添加數據。 這兩個變量將添加到下一個空行的前兩個單元格中。 在這里,您將找到有關如何使用SpreadsheetApp的文檔和指南。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM