谷歌表格 - 如何将数据收集到不同的表格

时间:2014-10-01 14:11:55

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

我有一个google表单,可以将数据收集到电子表格中(表单响应1)。 因为我认为存在4m行的限制,我如何以编程方式创建新工作表并将响应存储到新创建的工作表中。

注意:这不是将响应复制到新表中。

1 个答案:

答案 0 :(得分:0)

使用“新表格”,电子表格中的大小限制大约为2百万个单元格(不是400万行)。 Reference

Forms Apps Script Reference中提供了创建新电子表格并将其设置为表单目的地的示例:

// Open a form by ID and create a new spreadsheet.
 var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
 var ss = SpreadsheetApp.create('Spreadsheet Name');

 // Update the form's response destination.
 form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());

该脚本的这种更复杂的演变可以在满足某些预设目标时自动创建新文件(例如,每100个响应),并生成符合Google表单命名约定的新文件名:

/**
 * Check number of responses and switch to new destination if needed.
 * Set up as a trigger function to run on Form submission.
 */
function checkResponseCount(e) {
  var form = e.source;
  var numResponses = form.getResponses().length;

  // Start a new destination spreadsheet after each 100 responses
  if (numResponses % 100 == 0) {
    startNewDestinationSpreadsheet();
  }
}

/**
 * Google Form script to start a new spreadsheet in the user's "root"
 * directory, for the form's responses.
 *
 * If no new name is provided, it will be automatically created based on the
 * name of the current spreadsheet destination, by incrementing the counter
 * in a standard destination name, e.g. "My Form (Responses 5)".
 *
 * @param {string}    OPTIONAL New spreadsheet name.
 *
 * @returns {string}           ID of new spreadsheet
 */
function startNewDestinationSpreadsheet(ssName) {
  var form = FormApp.getActiveForm();
  var curDest = DriveApp.getFileById(form.getDestinationId());
  var curName = curDest.getName();

  // Expect that current name is either in standard form (ENGLISH - adjust as needed)
  // or that a new name has been provided as ssName.
  if (!ssName || ssName === '') {
    if (curName.search(/\(Responses.*\)/) == -1) {
      ssName = curName+" (Responses 2)";
    }
    else if (curName.indexOf("(Responses)") !== -1) {
      ssName = curName.replace("Responses","Responses 2");
    }
    else {
      // Increment number
      ssName = curName.replace(/(\d+)+/g, function(match, number) {
         return parseInt(number)+1;
      });
    }
  }

  // Create new spreadsheet, and set as form destination
  var newSheetId = SpreadsheetApp.create(ssName).getId();
  form.setDestination(FormApp.DestinationType.SPREADSHEET, newSheetId);

  // The new destination just had ALL existing responses copied to it - wipe them
  Utilities.sleep(10000); // Wait for sheet to be updated
  var sheet = SpreadsheetApp.openById(newSheetId).getActiveSheet();
  sheet.deleteRows(2, sheet.getMaxRows()-2);  // Cannot delete all non-frozen rows, so leave one

  return newSheetId;
}

警告Emptor

但是有一个问题。将电子表格设置为表单的目标时,所有现有响应都将复制到新目标中。如果您担心目标电子表格中的空间不足,那么这显然无济于事。

正因为如此,你不想的答案 - 将答复复制到另一张表 - 真的是你最好的选择。