简单的Google表宏以复制paste更新公式输出到不同的单元格

发布于 2025-02-13 05:41:55 字数 1158 浏览 1 评论 0 原文

我正在尝试做一个非常基本的副本粘贴宏,其中:

  • 将单元格中的值
  • 通过公式更改为公式,单元i20更新中的值
  • 复制单元i20中的值粘贴到j20到j20

,然后再次完成周期:

  • 更改值单元格H20(第一次有不同的值 在周围)
  • 通过公式,单元i20中的值复制了
  • 单元i20中的值粘贴到J21

,然后再次将其粘贴到J22中。

我发现的是,一旦宏运行,小区j20,j21和j22中的值是相同的,何时应不同,因为在过程中i20中的值会更改。 这几乎就像它在最后一次粘贴了i20中的值,而不是整个三个单元格。

有人知道为什么这是什么以及我如何解决?我通过录制而不是编码来制作这个宏。

  var evendersheet = dreversheetapp.getactive();
  电子表格。getRange('h20')。activate();
  电子表格。getCurrentCell()。setValue('2');
  电子表格。getRange('j20')。activate();
  evredsheet.getRange('i20')。copyto(everdsheet.getActiverange(),vredsheetapp.copypastepe.paste_values,false);
  电子表格。getRange('h20')。activate();
  vendersheet.getCurrentCell()。setValue('3');
  电子表格。getRange('j21')。activate();
  evredsheet.getRange('i20')。copyto(everdsheet.getActiverange(),vredsheetapp.copypastepe.paste_values,false);
  电子表格。getRange('h20')。activate();
  vendersheet.getCurrentCell()。setValue('4');
  电子表格('j22')。activate();
  evredsheet.getRange('i20')。copyto(everdsheet.getActiverange(),vredsheetapp.copypastepe.paste_values,false);
 

I am trying to do a very basic copy paste macro where it:

  • Changes the value in cell H20
  • Through a formula, the value in cell I20 updates
  • Copy Pastes the value in cell I20 to J20

It then completes the cycle again:

  • Changes the value in cell H20 (a different value to the first time
    around)
  • Through a formula, the value in cell I20 updates
  • Copy Pastes the value in cell I20 to J21

And once again into J22.

What I am finding is that once the macro has run, the value in cell J20, J21 and J22 is the same, when they should be different, since the value in I20 changes during the process.
It's almost like it's pasting the value in I20 at once to all three cells at the end, rather than throughout.

Does anyone know why this is and how I can fix it? I made this macro by recording it, not coding.

  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('H20').activate();
  spreadsheet.getCurrentCell().setValue('2');
  spreadsheet.getRange('J20').activate();
  spreadsheet.getRange('I20').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  spreadsheet.getRange('H20').activate();
  spreadsheet.getCurrentCell().setValue('3');
  spreadsheet.getRange('J21').activate();
  spreadsheet.getRange('I20').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  spreadsheet.getRange('H20').activate();
  spreadsheet.getCurrentCell().setValue('4');
  spreadsheet.getRange('J22').activate();
  spreadsheet.getRange('I20').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻拂→两袖风尘 2025-02-20 05:41:55

您需要在每个变化之间刷新表格

function test(){
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('H20').activate();
  spreadsheet.getCurrentCell().setValue('2');
  spreadsheet.getRange('J20').activate();
  spreadsheet.getRange('I20').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  SpreadsheetApp.flush()
  spreadsheet.getRange('H20').activate();
  spreadsheet.getCurrentCell().setValue('3');
  spreadsheet.getRange('J21').activate();
  spreadsheet.getRange('I20').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  SpreadsheetApp.flush()
  spreadsheet.getRange('H20').activate();
  spreadsheet.getCurrentCell().setValue('4');
  spreadsheet.getRange('J22').activate();
  spreadsheet.getRange('I20').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}

应用所有等待的电子表格更改。

说明

ELI5类比如何:

据我所知,当您运行脚本时,Google拍摄了表格并根据其运行。因为您有一个基于输入的价值的公式,并且由于您采取结果,因此必须告诉Google:刷新工作表并拍摄新图像。

You need to refresh your sheet between each changes of value

function test(){
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('H20').activate();
  spreadsheet.getCurrentCell().setValue('2');
  spreadsheet.getRange('J20').activate();
  spreadsheet.getRange('I20').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  SpreadsheetApp.flush()
  spreadsheet.getRange('H20').activate();
  spreadsheet.getCurrentCell().setValue('3');
  spreadsheet.getRange('J21').activate();
  spreadsheet.getRange('I20').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  SpreadsheetApp.flush()
  spreadsheet.getRange('H20').activate();
  spreadsheet.getCurrentCell().setValue('4');
  spreadsheet.getRange('J22').activate();
  spreadsheet.getRange('I20').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}

flush() Applies all pending Spreadsheet changes.

explanation

How about an ELI5 analogy:

as far as i know, when you run the script, google takes an image of the sheet and runs based on it. Because you have a formula based on the value you entered, and because you take the result, you have to tell google: refresh the sheet and take a new image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文