You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于Google Form用户数在关联Sheet生成多行并应用公式?

Generate Dynamic Rows from Google Form Responses in Google Sheets

Hey there! I get that as a newbie, figuring out dynamic row generation with form data and existing formulas can feel tricky—let's break this down step by step using Google Apps Script, which will handle the heavy lifting automatically.

Here's the plan:

We'll create a script that reads the "用户数量" (Number of Users) value from your form-linked sheet, then generates that many rows in your target worksheet, copies over associated data (like Location ID), and applies your existing formulas to those new rows.


Step 1: Open the Apps Script Editor

  1. Open your Google Sheet linked to the Google Form.
  2. Click Extensions > Apps Script to launch the script editor. You'll see a default Code.gs file in a new tab.

Step 2: Replace the Default Code

Delete the existing code and paste this custom script (I added comments to explain each part so you can tweak it easily):

function generateUserRows() {
  // Replace with your actual sheet names
  const sourceSheetName = "Form Responses 1"; // Name of your form-linked sheet
  const targetSheetName = "User Data"; // Name of your sheet where rows will be generated
  
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sourceSheet = ss.getSheetByName(sourceSheetName);
  const targetSheet = ss.getSheetByName(targetSheetName);
  
  // Get the latest form response row
  const lastSourceRow = sourceSheet.getLastRow();
  if (lastSourceRow === 1) { // No form responses yet
    SpreadsheetApp.getUi().alert("No form responses found!");
    return;
  }
  
  // Adjust column indices (A=1, B=2, etc.) to match your sheet
  const locationIDCol = 1; // Column for Location ID
  const userCountCol = 2; // Column for Number of Users
  
  // Pull data from the latest form entry
  const locationID = sourceSheet.getRange(lastSourceRow, locationIDCol).getValue();
  const userCount = sourceSheet.getRange(lastSourceRow, userCountCol).getValue();
  
  if (userCount < 1) { // Skip if user count is invalid
    SpreadsheetApp.getUi().alert("User count must be at least 1!");
    return;
  }
  
  // Find the first empty row in the target sheet
  const firstEmptyRow = targetSheet.getLastRow() + 1;
  
  // Insert the required number of rows
  targetSheet.insertRowsAfter(firstEmptyRow - 1, userCount);
  
  // Fill Location ID for all new rows
  targetSheet.getRange(firstEmptyRow, locationIDCol, userCount, 1).setValue(locationID);
  
  // Copy existing formulas from your base row (assuming row 2 is the first data row with formulas)
  const formulaRow = 2;
  const lastColumn = targetSheet.getLastColumn();
  
  // Copy formulas to the new rows
  const formulaRange = targetSheet.getRange(formulaRow, 1, 1, lastColumn);
  formulaRange.copyTo(targetSheet.getRange(firstEmptyRow, 1, userCount, lastColumn), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
  
  SpreadsheetApp.getUi().alert(`Successfully generated ${userCount} rows!`);
}

Step 3: Customize the Script for Your Sheet

  • Update sourceSheetName and targetSheetName to match your actual sheet names.
  • Adjust locationIDCol and userCountCol to the correct column numbers (A=1, B=2, etc.) where your data lives.
  • If your base formulas are in a row other than row 2, change formulaRow to that row number.

Step 4: Test the Script

  1. Save the script by clicking the floppy disk icon, give it a name like "GenerateUserRows".
  2. Click the run button (▶️) to execute it. The first time you run it, you'll need to grant permissions—follow the prompts, and you may need to click "Advanced" then "Go to [Script Name]" to allow access.
  3. Check your target sheet—you should see the new rows with Location ID and your formulas applied!

Step 5: Automate It (Optional)

To make this run automatically whenever a new form response is submitted:

  1. In the Apps Script editor, click the clock icon (Triggers) on the left sidebar.
  2. Click Add Trigger.
  3. Set these options:
    • Choose which function to run: generateUserRows
    • Choose which deployment to run: Head
    • Select event source: From spreadsheet
    • Select event type: On form submit
  4. Click Save. Now every new form entry will trigger row generation automatically!

Quick Tips for Beginners

  • Always make a backup of your sheet before running scripts, just in case.
  • Relative formulas (like =C2+1) will adjust automatically when copied to new rows—perfect for your use case!
  • If you get errors, double-check that your sheet names and column numbers are correct.

内容的提问来源于stack exchange,提问作者Bret

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:57:23