如何将带有Google Spreadsheet的Google Form复制到新所有者

时间:2019-03-23 22:59:31

标签: google-apps-script google-sheets google-form

我需要修复脚本的帮助。目前,我有一个脚本,使我能够从主表单复制工作表。

问题:
如果我有链接到工作表的表单,则脚本不会复制该表单。我想复制表格并链接到每个工作表副本。


我发现了wonderful app script(感谢youtuber Carl Arrow Smith),使我能够从主表单中复制表格了!这是代码:

function copyfilefromsource() {
  var ui = SpreadsheetApp.getUi();

  var sheet_merge = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("List");
  var last_row = sheet_merge.getLastRow();

  var newsheet_firstname = null;
  var newsheet_surname = null;
  var newsheet_email = null;
  var source_folder = null;
  var file_owner = null;
  var dest_folder = null;
  var sheets_created = 0;
  var new_file = null;
  var source_file = null;
  var student_id = null;
  var google_domain = null;
  var range = sheet_merge.getRange(1, 1, last_row, 4);
  var temp = null;

  source_file = DriveApp.getFileById(range.getCell(1, 2).getValue()); //gets the source file to copy  
  dest_folder = DriveApp.getFolderById(range.getCell(2, 2).getValue()); //gets the ID of the folder to place the copied files into
  file_owner = range.getCell(3, 2).getValue(); //person to own the files
  google_domain = range.getCell(4, 2).getValue(); //extension of the email address 

  if (last_row <= 6) {
    SpreadsheetApp.getUi().alert("No rows to process!");
    return
  }

  for (var i = 7; i <= last_row; i++) { // loop to go down evenery row from 7 until the end
    if (range.getCell(i, 4).getValue() == '') { //makes sure there is nothing in the column, i.e. can run the script with some students already been processed if it fails half way through.
      student_id = range.getCell(i, 1).getValue(); //take the first column for the     
      newsheet_email = student_id + google_domain; //combines with username with the domain; 
      // should add some user checking code here!!!!! and only run the below if user exists (future improvement)

      newsheet_firstname = range.getCell(i, 2).getValue(); //gets first name from the sheet
      newsheet_surname = range.getCell(i, 3).getValue(); //gets surname from the sheet

      new_file = source_file.makeCopy(source_file.getName() + " - " + newsheet_firstname + " " + newsheet_surname + " - " + student_id, dest_folder);
      new_file.setOwner(file_owner); //allocates an owner to the sheet
      new_file.addViewer(newsheet_email); //allocates a viewer to the sheet
      //new_file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW); //adds anyone with the link as a viewer

      //source_folder.removeFile(new_file); // hides from root folder  not required as going directly to correrrect folder
      // new_file.setActiveSheet('My Grades');
      //new_file.getRange(2,3).setValue(student_id);

      //change the a cell in the copied spreadsheet to the student ID 
      //var ss = SpreadsheetApp.openById(new_file.getId());
      //var sheet = ss.getSheets()[0]; // "access data on different tabs"
      //var sheet = ss.getSheetByName('My Grades');
      // ss.setActiveSheet(sheet);
      // ss.getRange('C2').setValue(student_id);

      //Add the details that the process has worked
      SpreadsheetApp.getActiveSheet().getRange(i, 1).setFormula('=HYPERLINK("' + new_file.getUrl() + '/","' + student_id + '")');
      SpreadsheetApp.getActiveSheet().getRange(i, 4).setValue(new_file.getId());

      sheets_created++; // add to spreadsheets created
    }
  }
  ui.alert(sheets_created + " files were created!")
}

但是我有一个链接到工作表的表单,该脚本没有复制该表单。

我想复制表格并链接到每个工作表副本。

App脚本实质上链接到工作表。源文件和文件夹将添加到工作表中。每次在工作表中输入人员姓名和电子邮件的第一部分(以下副本)时,都会创建工作表的副本。我的问题是如何添加到脚本以获取要复制的表单链接。

链接到我的spreadsheet


我在StackOverflow上找到了第二个脚本,该脚本旨在解决类似的问题(Copying Google Spreadsheet no longer copies linked Form),并且我尝试向其中添加脚本,但现实是48小时后,我只是一团糟而没有前进完全没有

这里的第一篇文章,如果我没有提供足够的信息,请对我轻松一点。 谢谢

最终结果必须为表格,并链接以与工作表一起复制

0 个答案:

没有答案