Forms 和 Sheets 上的两个 App 脚本 运行,需要将它们都连接起来
Two App scripts running on Forms and Sheets, need to connect them both
我有一个入职表格,将所有回复放在 google sheet 中,其中有一个应用程序脚本 运行 将用户添加到 google 管理员和不同的组从 sheet 的最后一行获取值。这很好用,只是每次填写表单时我都必须 运行 脚本,所以我想创建一个表单触发器。
在附加到 google 表单的应用程序脚本上创建表单提交触发器是有意义的,我添加了其他应用程序脚本的库和脚本 ID,并从那里提取了一个方法,例如
// Create a form submit installable trigger
// using Apps Script.
function createFormSubmitTrigger() {
// Get the form object.
var form = FormApp.getActiveForm();
// Since we know this project should only have a single trigger
// we'll simply check if there are more than 0 triggers. If yes,
// we'll assume this function was already run so we won't create
// a trigger.
var currentTriggers = ScriptApp.getProjectTriggers();
if(currentTriggers.length > 0)
return;
// Create a trigger that will run the onFormSubmit function
// whenever the form is submitted.
ScriptApp.newTrigger("onFormSubmit").forForm(form).onFormSubmit().create();
}
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
function onFormSubmit() {
wait(7000);
AddingUserAutomation.createUserFromSheets()
}
问题是我得到了错误
TypeError: Cannot read property 'getLastRow' of null
at createUserFromSheets(Code:43:19)
我的 createUserFromSheets 函数正在使用 sheet
function createUserFromSheets(){
let data = SpreadsheetApp.getActiveSheet();
let row = data.getLastRow();
let firstname = data.getRange(row,2).getValue();
let lastname = data.getRange(row,3).getValue();
... etc etc
}
我认为它无法提取 getActiveSheet 部分,这就是为什么我在 formSubmit() 上添加了 wait() 函数但它仍然无法工作的原因。
是否有解决此问题的方法或更好的方法?
function createWorkspaceUser(recentResponse) {
console.log("Creating account for:\n"+recentResponse[1]);
debugger;
var user = {"primaryEmail": recentResponse[0] + '.' + recentResponse[1] + '@' + recentResponse[3],
"name": {
"givenName": recentResponse[0],
"familyName": recentResponse[1]
},
"password": newPassword(),
};
try {
user = AdminDirectory.Users.insert(user);
console.log('User %s created with ID %s.', user.primaryEmail, user.id);
}catch(err) {
console.log('Failed with error %s', err.message);
}
}
我正在这样做,但是 运行在 primemail
上出现错误
建议[新更新]
As mentioned by RemcoE33
要获得更简单的设置,或许可以跳过库部分并在 Google 表单 中执行所有脚本 (bound script)。
因为我们没有您实际 Google 表格的完整概览。请参阅下面的示例作为参考:
Google 表单脚本
function onFormSubmit() {
var form = FormApp.getActiveForm();
var count = 0;
var recentResponse = [];
var formResponses = form.getResponses();
for (var i in formResponses) {
count += 1;
var formResponse = formResponses[i];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
if(formResponses.length === count){ //Process only the recently submitted response
var itemResponse = itemResponses[j];
recentResponse.push(itemResponse.getResponse())
}
}
}
createWorkspaceUser(recentResponse);
}
function createWorkspaceUser(recentResponse){
var user = {"primaryEmail": recentResponse[0].replace(/\s/g, '') + '.' + recentResponse[1].replace(/\s/g, '') + '@' +recentResponse[3],
"name": {
"givenName": recentResponse[0],
"familyName": recentResponse[1]
},
"password":newPassword(),
};
try{
user = AdminDirectory.Users.insert(user);
Logger.log('User %s created with ID %s.', user.primaryEmail, user.id);
}catch (err) {
Logger.log('Failed with error %s', err.message);
}
console.log(user);
}
NOTE: You no longer need to build an on form submit
trigger since the onFormSubmit()
function will automatically run right after hitting the submit button.
示范[=28=]
1.从示例表单提交用户数据:
2。将在 Workspace 管理控制台上创建测试用户帐户用户:
参考
我有一个入职表格,将所有回复放在 google sheet 中,其中有一个应用程序脚本 运行 将用户添加到 google 管理员和不同的组从 sheet 的最后一行获取值。这很好用,只是每次填写表单时我都必须 运行 脚本,所以我想创建一个表单触发器。
在附加到 google 表单的应用程序脚本上创建表单提交触发器是有意义的,我添加了其他应用程序脚本的库和脚本 ID,并从那里提取了一个方法,例如
// Create a form submit installable trigger
// using Apps Script.
function createFormSubmitTrigger() {
// Get the form object.
var form = FormApp.getActiveForm();
// Since we know this project should only have a single trigger
// we'll simply check if there are more than 0 triggers. If yes,
// we'll assume this function was already run so we won't create
// a trigger.
var currentTriggers = ScriptApp.getProjectTriggers();
if(currentTriggers.length > 0)
return;
// Create a trigger that will run the onFormSubmit function
// whenever the form is submitted.
ScriptApp.newTrigger("onFormSubmit").forForm(form).onFormSubmit().create();
}
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
function onFormSubmit() {
wait(7000);
AddingUserAutomation.createUserFromSheets()
}
问题是我得到了错误
TypeError: Cannot read property 'getLastRow' of null
at createUserFromSheets(Code:43:19)
我的 createUserFromSheets 函数正在使用 sheet
function createUserFromSheets(){
let data = SpreadsheetApp.getActiveSheet();
let row = data.getLastRow();
let firstname = data.getRange(row,2).getValue();
let lastname = data.getRange(row,3).getValue();
... etc etc
}
我认为它无法提取 getActiveSheet 部分,这就是为什么我在 formSubmit() 上添加了 wait() 函数但它仍然无法工作的原因。
是否有解决此问题的方法或更好的方法?
function createWorkspaceUser(recentResponse) {
console.log("Creating account for:\n"+recentResponse[1]);
debugger;
var user = {"primaryEmail": recentResponse[0] + '.' + recentResponse[1] + '@' + recentResponse[3],
"name": {
"givenName": recentResponse[0],
"familyName": recentResponse[1]
},
"password": newPassword(),
};
try {
user = AdminDirectory.Users.insert(user);
console.log('User %s created with ID %s.', user.primaryEmail, user.id);
}catch(err) {
console.log('Failed with error %s', err.message);
}
}
我正在这样做,但是 运行在 primemail
上出现错误建议[新更新]
As mentioned by RemcoE33
要获得更简单的设置,或许可以跳过库部分并在 Google 表单 中执行所有脚本 (bound script)。
因为我们没有您实际 Google 表格的完整概览。请参阅下面的示例作为参考:
Google 表单脚本
function onFormSubmit() {
var form = FormApp.getActiveForm();
var count = 0;
var recentResponse = [];
var formResponses = form.getResponses();
for (var i in formResponses) {
count += 1;
var formResponse = formResponses[i];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
if(formResponses.length === count){ //Process only the recently submitted response
var itemResponse = itemResponses[j];
recentResponse.push(itemResponse.getResponse())
}
}
}
createWorkspaceUser(recentResponse);
}
function createWorkspaceUser(recentResponse){
var user = {"primaryEmail": recentResponse[0].replace(/\s/g, '') + '.' + recentResponse[1].replace(/\s/g, '') + '@' +recentResponse[3],
"name": {
"givenName": recentResponse[0],
"familyName": recentResponse[1]
},
"password":newPassword(),
};
try{
user = AdminDirectory.Users.insert(user);
Logger.log('User %s created with ID %s.', user.primaryEmail, user.id);
}catch (err) {
Logger.log('Failed with error %s', err.message);
}
console.log(user);
}
NOTE: You no longer need to build an
on form submit
trigger since theonFormSubmit()
function will automatically run right after hitting the submit button.
示范[=28=]
1.从示例表单提交用户数据:
2。将在 Workspace 管理控制台上创建测试用户帐户用户: