我创建了一个脚本,将OU中的用户列表写入电子表格,这很有效。但是现在我想编写脚本,将多个OU的数据写入同一个电子表格,但是'&&'运营商似乎没有工作。我尝试使用OR' ||"运营商,但似乎也不起作用。有什么想法吗?
function listAllUserRR() {
var sh = '1Or1yr9JtwsEvg97U1VjX4XNEZylxoVPwRzJXC6wZDKx';
var sheet = SpreadsheetApp.openById(sh);
var sheet1 = sheet.getSheetByName('Google Users');
var sheet1range = sheet.getRange("A:F")
sheet1range.clear()
var data = [];// array to store values
data.push(['Email' ,'Firstname', 'Lastname', 'OU', 'Suspended', 'LastLoginTime']);// store headers
var pageToken, page;
do {
page = AdminDirectory.Users.list({
customer: 'C00ont7ej',
query: "OrgUnitPath='/Brazil'" || "orgUnitPath='/Argentina'",
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.orgUnitPath, user.suspended, user.lastLoginTime, ]);//store in an array of arrays (one for each row)
}
} 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'));
}
答案 0 :(得分:2)
这一行错了:
if (!Directory.Exists(copyPath))
{
File.Copy(filefullpath, copyPath);
File.Delete(filefullpath);
}
因为您在两个字符串之间使用了javascript运算符。 Javascript会做:
如果第一个字符串不是falsey(不是0,null,undefined,false或空),它将返回第一个字符串。或者如果没有,它将返回第二个字符串。例如:
query: "OrgUnitPath='/Brazil'" || "orgUnitPath='/Argentina'",
这绝对不是你想要的。你只想要这样的东西:
var query = "OrgUnitPath='/Brazil'" || "orgUnitPath='/Argentina'";
console.log(query);
// Prints: OrgUnitPath='/Brazil'
var query = "" || "orgUnitPath='/Argentina'";
console.log(query);
// Prints: orgUnitPath='/Argentina'
另请注意,您使用的是var query = "OrgUnitPath='/Brazil' OR orgUnitPath='/Argentina'";
console.log(query);
// Prints: OrgUnitPath='/Brazil' OR orgUnitPath='/Argentina'
和orgUnitPath
(大写和小写OrgUnitPath
)。我不知道查询是否区分大小写,但无论如何都要保持一致......