Google 应用脚本,突出显示自上个月以来的新用户

Google App Script, highlight new users since last month

我做了一个小功能来检索我们在 Google Workspace 上的用户,并为这个列表做了一个 cron,每月刷新一次。

我现在想做的是突出显示上个月创建的新用户的单元格,但我还没有找到合适的方法。

即。计划的下一次执行将在 6 月 1 日,我需要突出显示 5 月创建的所有帐户

有什么提示吗?

我当前的代码如下所示:

function listAllUsers() {

 var sh = '-- my sheet id --';
 var sheet = SpreadsheetApp.openById(sh);
 var sheet1 = sheet.getSheetByName('sheet title');
  
  var sheet1range = sheet.getRange("A:H")
  sheet1range.clear()
 
  var data = [];
  data.push(['Email' ,'First Name', 'Last Name',  'Suspended', 'Last Login Time','Creation', '2FA Active', 'Is admin']);
  var pageToken, page;
  do {
    page = AdminDirectory.Users.list({
    
      domain: '-- my domain --',
      pageToken: pageToken
    });
    var users = page.users;
    if (users) {
      for (var i = 0; i < users.length; i++) {
        var user = users[i];
        data.push([user.primaryEmail, user.name.givenName, user.name.familyName, user.suspended, user.lastLoginTime , user.creationTime, user.isEnrolledIn2Sv, user.isAdmin ]);
      }
    } else {
    Logger.log('No users found.');
       }
    pageToken = page.nextPageToken;
  } while (pageToken);
  
  sheet1.getRange(1,1,data.length,data[0].length).setValues(data);
  var dated = sheet.getRange("P1")
  dated.setValue(Utilities.formatDate(new Date(),Session.getScriptTimeZone(),'dd-MMM-yyy'));
  
}

我相信你的目标如下。

  • 您的电子表格有第一行 header 和数据行。 “F”列具有用户帐户创建时间的值。
  • 您想为“F”列设置特定月份的单元格(“A”到“H”列)的背景颜色。
  • 您想使用 Google Apps 脚本实现此目的。

示例脚本:

function sample() {
  var checkYear = 2022; // Please set year.
  var checkMonth = 5; // Please set month. This sample is May.
  var color = "red"; // Please set the color you want to set.

  var sh = '-- my sheet id --';
  var sheet = SpreadsheetApp.openById(sh);
  var sheet1 = sheet.getSheetByName('sheet title');

  var range = sheet1.getRange("A2:H" + sheet1.getLastRow());
  var backgrounds = range.getValues().map(r => Array(r.length).fill(r[5].getFullYear() == checkYear && r[5].getMonth() == checkMonth - 1 ? color : null));
  range.setBackgrounds(backgrounds);
}
  • 当此脚本为运行时,通过检查“F”列的值,“A”至“H”列的背景颜色更改为“红色”。

  • 当你想在你的放映脚本中包含这个脚本时,如何进行以下修改?在这种情况下,请将以下脚本添加到您的函数底部。

      var checkYear = 2022; // Please set year.
      var checkMonth = 5; // Please set month. This sample is May.
      var color = "red"; // Please set the color you want to set.
    
      var range = sheet1.getRange("A2:H" + sheet1.getLastRow());
      var backgrounds = range.getValues().map(r => Array(r.length).fill(r[5].getFullYear() == checkYear && r[5].getMonth() == checkMonth - 1 ? color : null));
      range.setBackgrounds(backgrounds);
    

参考文献:

已添加:

根据您的以下回复,

Thanks for the hint ! You basically got the idea, but there's still need to do some manual settings in your example. As I want to run this script in a crontab and not care about it anymore, I would need to skip the checkYear and checkMonth config part. Is there any way to define these two vars automatically ?

如果你想在这个月之前设置单元格的背景颜色,下面的示例脚本怎么样?

示例脚本:

function sample() {
  var color = "red"; // Please set the color you want to set.

  var date = new Date();
  var checkYear = date.getFullYear();
  var checkMonth = date.getMonth();

  var sh = '-- my sheet id --';
  var sheet = SpreadsheetApp.openById(sh);
  var sheet1 = sheet.getSheetByName('sheet title');
 
  var range = sheet1.getRange("A2:H" + sheet1.getLastRow());
  var backgrounds = range.getValues().map(r => {
    if (r[5]) {
      var d = new Date(r[5]);
      return Array(r.length).fill(d.getFullYear() == checkYear && d.getMonth() == checkMonth ? color : null)
    }
    return Array(r.length).fill(null);
  });
  range.setBackgrounds(backgrounds);
}