【问题标题】:Google Sheets Macro not pasting formula谷歌表格宏不粘贴公式
【发布时间】:2021-06-03 22:44:16
【问题描述】:

请帮助使用 Google 表格宏。尝试简单的东西作为开始。宏需要清除 B2 单元格并粘贴公式(importhtml),如下所示。然后它必须对单元格 B43 执行相同的操作(如果可行,可能会在此工作表上的多个单元格中执行此操作)。

Google 表格链接: https://docs.google.com/spreadsheets/d/1O0AJJEd0tGyFXlEPhih8HG9YOWcZNRot_Z7FXPdQrx8/edit?usp=sharing

奇怪的是它在 B43 中有效,但 B2 为空。

 function Untitledmacro() {

// Clear B2 and then paste formula

  var spreadsheet = SpreadsheetApp.getActive();

  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('DATA'), true);
  spreadsheet.getRange('B2').activate();

  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
};

  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('DATA'), true);
  spreadsheet.getRange('B2').activate();

  spreadsheet.getCurrentCell().setFormula('=IMPORTHTML("https://www.investing.com/commodities/real-time-futures","table",1)');

// Clear B43 and then paste formula


  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('DATA'), true);
  spreadsheet.getRange('B43').activate();

  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});

  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('DATA'), true);
  spreadsheet.getRange('B43').activate();

  spreadsheet.getCurrentCell().setFormula('=IMPORTHTML("https://www.investing.com/indices/indices-futures","table",1)');



  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('DATA'), true);

Added Sheets API, not sure it's needed here though

【问题讨论】:

  • 您好 David,我们无法访问您共享的电子表格。请修正权限。

标签: google-sheets google-sheets-formula


【解决方案1】:

看看有没有帮助

function clearAndSetformula() {

  const sheet = SpreadsheetApp.getActive().getSheetByName('DATA');
  const cells = ["B2", "B43"];
  const formulas = [
    '=IMPORTHTML("https://www.investing.com/commodities/real-time-futures","table",1)',
    '=IMPORTHTML("https://www.investing.com/indices/indices-futures","table",1)'
  ];
  cells.forEach((cell, i) => sheet.getRange(cell).clear({ contentsOnly: true, skipFilteredRows: true }).setFormula(formulas[i]));
  };

如果您需要添加更多单元格/公式,请将它们添加到适当的数组中。确保位置对齐,以便数组单元格中的第一个单元格将具有数组公式中的第一个公式等...

希望对你有帮助?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多