是否有一个脚本可以在通过 Google 表单输入新条目时自动在 Google 表格中填充 VLOOKUP 公式?

问题描述 投票:0回答:1

每次通过 google forms 输入新条目时,我都会尝试让 google 表格用

=VLOOKUP(K:K,Lookup!C:F,4,true)
填充列 B
起初,我尝试将其作为表单本身中的预填充答案,但是 forms
'
前面添加了
=
,从而禁用了公式。
然后我尝试了很多脚本。

function onEdit(e) {
  var editedRange = e.range;
  var sheet = editedRange.getSheet();
  
  // Check if the edited range is in column A
  if (editedRange.getColumn() === 1) {
    var row = editedRange.getRow();
    var value = editedRange.getValue();
    var formulaRange = sheet.getRange(row, 2);
    
    // Check if the corresponding cell in column A is not empty
    if (value !== '') {
      var currentFormula = formulaRange.getFormula();
      var targetFormula = '=VLOOKUP(K:K,Lookup!C:F,4,true)';
      
      // Set the formula in the formula range if it is not already set
      if (currentFormula !== targetFormula) {
        formulaRange.setFormula(targetFormula);
      }
    } else {
      // Clear the formula in the formula range
      formulaRange.clearContent();
    }
  }
}

**在键入时填充 b 列,但在提交表单时不填充。 **

function onFormSubmit(e) {
  var sheet = e.range.getSheet();
  var row = e.range.getRow();
  var value = e.values[0]; // Assuming the form response is in the first column (column A)
  var formulaRange = sheet.getRange(row, 2); // Corresponding cell in column B
  
  // Check if the corresponding cell in column A is not empty
  if (value !== '') {
    var currentFormula = formulaRange.getFormula();
    var targetFormula = '=VLOOKUP(K:K,Lookup!C:F,4,true)';
    
    // Set the formula in the formula range if it is not already set
    if (currentFormula !== targetFormula) {
      formulaRange.setFormula(targetFormula);
    }
  } else {
    // Clear the formula in the formula range
    formulaRange.clearContent();
  }
}

不起作用

function autoFillFormula() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var form = FormApp.getActiveForm();
  var formResponses = form.getResponses();

  for (var i = 0; i < formResponses.length; i++) {
    var formResponse = formResponses[i];
    var itemResponses = formResponse.getItemResponses();

    // Assuming the timestamp is in the first column (column A)
    var timestamp = formResponse.getTimestamp().getTime();

    // Find the corresponding row based on the timestamp in column A
    var row = sheet.createTextFinder(String(timestamp)).findNext().getRow();
    var formulaRange = sheet.getRange(row, 2); // Corresponding cell in column B

    // Check if the cell in column B is empty
    if (formulaRange.getValue() === '') {
      formulaRange.setFormula('=VLOOKUP(K:K,Lookup!C:F,4,true)');
    }
  }
}

不起作用

现在你可能可以看出编码不是我的事,我一直在尝试使用 ChatGPT 来编码。
希望得到一些帮助。 :)

forms google-sheets google-forms
1个回答
0
投票

为什么不使用 arrayformula + importrange?

我认为您将 Google 表单结果存储在 Google 表格中?如果是这样,您将需要一个导入范围公式将所有 Google 表单结果导入到您正在使用的 Google 表格中:

=importrange("[URL of Google Form results sheet]", "[tab + range of results]")

当 Google 表单中有新条目时,此 arrayformula 公式可以自动填充 vlookup:

=arrayformula(if(K:K<>"", vlookup(K:K,Lookup!C:F,4,true), "")
© www.soinside.com 2019 - 2024. All rights reserved.