在现有电子表格Google表格中创建新表格

时间:2018-09-10 12:47:31

标签: google-apps-script google-sheets

这是我当前项目中的第三个(也是最后一个)问题。

在以下代码中,IF ELSE语句似乎无法正常工作:

function onFormSubmit() {
  // onFormSubmit
  // get submitted data
  var ss = 
SpreadsheetApp.openById("1UMSTyjYz2DMubBT54Q7a0V5ie5TAPYFZ2riqV5p93gE");
  var sheet = ss.getSheetByName("Submissions");
  var row = sheet.getLastRow();
  var Col = sheet.getLastColumn();
  var headings = sheet.getRange(1,1,1,Col).getValues();
  var lastRow = sheet.getRange(row, 1, 1, Col);
  var UnitNumber = sheet.getRange(row,3,Col).getValue(); 
  var newSheet = sheet.getRange(row,4,Col).getValue();
  // check if username has sheet

  if(ss.getSheetByName(UnitNumber)){
    var DrillSheet = ss.insertSheet(UnitNumber);
    // if not make
  } else {
  var DrillSheet = SpreadsheetApp.create(UnitNumber); 
DrillSheet.getSheetByName('Sheet1').getRange(1,1,1,Col).setValues(headings);
  }
  // Rename sheet to submission date
  DrillSheet.renameActiveSheet(newSheet);
  // copy submitted data to Drill sheet
  DrillSheet.appendRow(lastRow.getValues()[0]);
  DrillSheet.appendRow(['=CONCATENATE(B6," ",B5)']);
  DrillSheet.appendRow(['=TRANSPOSE(B1:2)']);
  //Hide top rows with raw data
  var hiderange = DrillSheet.getRange("A1:A3");
  DrillSheet.hideRow(hiderange);
  //Widen columns
  DrillSheet.setColumnWidth(1,390);
  DrillSheet.setColumnWidth(2,700);
}

此处的目标是创建一个新电子表格,如果该电子表格尚不存在,则以标题“ UnitNumber”和工作表标题作为提交日期。如果确实存在“ UnitNumber”电子表格,那么我想在该电子表格中创建一个新表,标题为提交日期。

当前,它每次都在创建一个新的电子表格。

3 个答案:

答案 0 :(得分:0)

看到问题后,请尝试以下代码:

function onFormSubmit() {
  var ss = SpreadsheetApp.openById("1UMSTyjYz2DMubBT54Q7a0V5ie5TAPYFZ2riqV5p93gE");
  var sheet = ss.getSheetByName("Submissions");
  var row = sheet.getLastRow();
  var Col = sheet.getLastColumn();
  var headings = sheet.getRange(1,1,1,Col).getValues();
  var lastRow = sheet.getRange(row, 1, 1, Col);
  var UnitNumber = sheet.getRange(row,3).getValue(); 

  var fileExist = false;
  var drillSheet = null;
  var folder = DriveApp.getFoldersByName("nameOfTheFolder").next();
  var files = folder.getFilesByName(UnitNumber);
  var file = null;
  while (files.hasNext())
  {
    fileExist = true;
    file = files.next();
    break;
  }
  if (fileExist)
  {
    drillSheet = SpreadsheetApp.openById(file.getId());
    drillSheet.renameActiveSheet("randomString") ;
  }
  else
  {
    drillSheet = SpreadsheetApp.create(UnitNumber);
    var ssID = drillSheet.getId();
    file = DriveApp.getFileById(ssID);
    file = file.makeCopy(UnitNumber, folder);
    DriveApp.getFileById(ssID).setTrashed(true);
    drillSheet = SpreadsheetApp.openById(file.getId());
    drillSheet.renameActiveSheet("randomString");
  }
}

答案 1 :(得分:0)

要回答最后一个问题,创建文件夹并与人员列表共享(整个文件夹)后,您随后放置在该文件夹中的任何文件都会自动与这些人员共享。

答案 2 :(得分:0)

这是固定的代码,可以正常使用。非常感谢@JSmith为我提供的帮助-没有您,我无法做到这一点!

function onFormSubmit() {
  // onFormSubmit
  // get submitted data and set variables
  var ss = SpreadsheetApp.openById("some id");
  var sheet = ss.getSheetByName("Submissions");
  var row = sheet.getLastRow();
  var Col = sheet.getLastColumn();
  var headings = sheet.getRange(1, 1, 1, Col).getValues();
  var lastRow = sheet.getRange(row, 1, 1, Col);
  var UnitNumber = sheet.getRange(row, 3).getValue(); 
  var newSheet = sheet.getRange(row, 4, Col).getValue();
  var fileExist = false;
  var drillSheet = null;
  var folder = DriveApp.getFoldersByName("Fraser Drill Inspections").next();
  var files = folder.getFilesByName(UnitNumber);
  var file = null;
  // check if Drill has sheet
  while (files.hasNext()) {
    fileExist = true;
    file = files.next();
    break;
  }
  if (fileExist) //If spreadsheet exists, insert new sheet
  {
    drillSheet = SpreadsheetApp.openById(file.getId());
    drillSheet.insertSheet("" + newSheet);
  }
  else //create new spreadsheet if one doesn't exist
  {
    drillSheet = SpreadsheetApp.create(UnitNumber);
    var ssID = drillSheet.getId();
    file = DriveApp.getFileById(ssID).makeCopy(UnitNumber, folder);
    DriveApp.getFileById(ssID).setTrashed(true);
    drillSheet = SpreadsheetApp.openById(file.getId());
    drillSheet.renameActiveSheet(newSheet);
  }
  // copy submitted data to Drill sheet
  drillSheet.getSheetByName(newSheet).getRange(1, 1, 1, Col).setValues(headings);
  drillSheet.appendRow(lastRow.getValues()[0]);
  drillSheet.appendRow(['=CONCATENATE(B6, " ", B5)']);
  drillSheet.appendRow(['=TRANSPOSE(B1:2)']);
  //Hide top rows with raw data
  var hiderange = drillSheet.getRange("A1:A3");
  drillSheet.hideRow(hiderange);
  //Widen columns
  drillSheet.setColumnWidth(1, 390);
  drillSheet.setColumnWidth(2, 700);
}