我尝试将该值从Google电子表格传递给JSON。但我得到一个无效的json字符串错误。你能帮我解决我做错的地方吗?
请找到下面的代码段
代码:
for(var i=0;i>=10;i++)
{
var userEmail = sheet.getRange(3, 2, sheet.getLastRow()).getValues();
var owner = {
email: userEmail[i],
role: 'OWNER'
};
owner = AdminDirectory.Members.insert(owner, groupEmail);
Logger.log('User %s added as a owner of group %s.', userEmail, groupEmail);
Browser.msgBox("Owner Successfully Added!");
}
答案 0 :(得分:0)
试试这个
var userEmail = sheet.getRange(3, 2, sheet.getLastRow() - 2, 1).getValues();
for(var i=0;i < userEmail.length; i++)
{
var owner = {
email: userEmail[i][0],
role: 'OWNER'
};
owner = AdminDirectory.Members.insert(owner, groupEmail);
Logger.log('User %s added as a owner of group %s.', userEmail, groupEmail);
Browser.msgBox("Owner Successfully Added!");
}
getRange
的第三个参数是要读取的行数,因为从第3行开始 - 省略前两行 - 要读取的行数减少2。
另外getValues
返回一个二维数组。
答案 1 :(得分:0)
var userEmail必须在for()循环之外,您还必须选择对象userEmail [i]的第一个值。 在你的代码中,我还假设在这段代码之前的某处定义了sheet var。
var userEmail = sheet.getRange(3, 2, sheet.getLastRow()).getValues();
for(var i in userEmail) {
var owner = {
email: userEmail[i][0],
role: 'OWNER'
};
owner = AdminDirectory.Members.insert(owner, groupEmail);
Logger.log('User %s added as a owner of group %s.', userEmail, groupEmail);
Browser.msgBox("Owner Successfully Added!");
}