Create a new sheet in an existing spreadsheet for

2019-06-14 13:37发布

I have created a Google form, and I need to create a new sheet in an existing spreadsheet for each response I receive from this form - each respondent generates a new sheet within the spreadsheet. Is this possible with some kind of onSubmit command? I'm completely new to Javascript but willing to learn. Thanks so much for your help.

2条回答
劳资没心,怎么记你
2楼-- · 2019-06-14 13:48

For this you can get the Spreadsheet using this command(getActiveSpreadsheet()) and then create a new sheet based on the parameters you require from this page. you can add this lines of code in your onSubmit trigger.

Hope that helps!

查看更多
干净又极端
3楼-- · 2019-06-14 13:52

You can't intercept a Google Form submission before the data is written into the spreadsheet. All you can do is manipulate the data after it's already been inserted into the spreadsheet. So, your responses will get put into the spreadsheet, all in one sheet to start with. You can't change that.

Now the question is, do you want to manipulate the data from code bound to the form or bound to the sheet? You'll want to use a project bound to the sheet.

This is some sample code:

This code assumes that the value you want to name your sheet is in column A function subTheForm() { Logger.log('submit ran');

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  //Get last row of data
  var lastRow = sheet.getLastRow();
  var colA_Data = sheet.getRange(lastRow, 1).getValue();

  //var thisUser = 'theUserName';
  ss.insertSheet(colA_Data);
};

Old answer:

You'll need to generate some kind of unique string every time a new form is submitted. I have no idea what you want. You could get the user name, then add the date/time to it, then name each new sheet the user name with the time. The basic code will look like this:

function submit(){
  Logger.log('submit ran');

  var thisUser = 'theUserName';
  var ss = SpreadsheetApp.getActive().insertSheet(thisUser);

};

Note the Logger.log(); statement. That prints something to the LOG. Inside the code editor, you can select the VIEW menu, and the LOGS menu item to show the LOG.

You can also run the code "manually" from the code editor by selecting the function name and clicking the run arrow icon. That way you can run the code without submitting a form every time. Get the code to work, then wire it up to the onsubmit trigger.

查看更多
登录 后发表回答