简体   繁体   English

如何在Google表格中将值从一个单元格转移到另一个单元格

[英]How To Transfer A Value From One Cell To Another In Google Sheets

I have an interesting situation where every second column in my Google Sheet represents the amount of items at various stations. 我有一个有趣的情况,我的Google表格中的第二列代表各个站点的项目数量。 What I'm trying to accomplish is to automatically adjust the amounts of these values when a transfer value is entered into a cell between these columns. 我要完成的工作是在将转移值输入到这些列之间的单元格时自动调整这些值的数量。

For example, here are the first few cells on the sheet: 例如,以下是工作表中的前几个单元格:

Cell B5: Data entered here should be added to the value in C5, then clear 单元格B5:此处输入的数据应添加到C5中的值,然后清除

Cell C5: Displays AMOUNT C 单元格C5:显示AMOUNT C

Cell D5: Data entered here should be subtracted from value in C5, added to the value in E5, then clear 单元格D5:应从C5中的值中减去此处输入的数据,将其添加到E5中的值中,然后清除

Cell E5: Displays AMOUNT E 单元格E5:显示AMOUNT E

See this attached example. 请参阅此附加示例。 Values are added to the yellow cells, and what they need to be doing to their adjacent cells is described above each one. 将值添加到黄色单元格,并在每个黄色单元格上方描述它们需要对相邻单元格执行的操作。

Some examples: 一些例子:

  1. Adding the number 5 to Cell B5 needs to change the value of C5 from 72 to 77, and then B5 needs to clear. 将数字5添加到单元格B5中需要将C5的值从72更改为77,然后需要清除B5。

  2. Adding the number 12 to J6 needs to change the value of I6 from 36 to 24, the value of K6 from nothing to 12, and then J8 needs to clear. 将数字12加到J6需要将I6的值从36更改为24,将K6的值从无更改为12,然后需要清除J8。

  3. Adding the number 32 to R5 needs to change the value of Q5 from 41 to 9, the value of S5 from 1 to 3, and then R5 needs to clear. 将数字32添加到R5需要将Q5的值从41更改为9,将S5的值从1更改为3,然后需要清除R5。

  4. Adding the number 5 to Cell V6 needs to change the value of U6 from 5 to nothing, and then V6 needs to clear. 在单元格V6中添加数字5需要将U6的值从5更改为空,然后需要清除V6。

I would prefer using formulas, and since I can avoid circular reference errors by going to File/Spreadsheet Settings/Calculation and changing the Max number of iterations to 1 (Threshold at 0.05), I can use a formula like =C5+B5 in Cell C5. 我更喜欢使用公式,因为我可以通过转到文件/电子表格设置/计算并将最大迭代次数更改为1(阈值为0.05)来避免循环引用错误,所以我可以在单元格中使用= C5 + B5这样的公式C5。 Is there a way to add to this formula so that B5 is cleared after its value is added to C5? 是否可以添加此公式,以便在将B5的值添加到C5之后清除B5? And how would I then add the functionality of subtracting a value entered in D5 from C5, and then clear D5 as well? 然后我该如何添加从C5中减去在D5中输入的值,然后再清除D5的功能?

Does anyone have any ideas? 有人有什么想法吗? It would be much appreciated! 不胜感激!

I think this onEdit() trigger is what you need. 我认为您需要此onEdit()触发器 Also see the documentations on Apps Script and Spreadsheet App . 另请参阅关于Apps脚本Spreadsheet App的文档。

For example, in your sheet, an implementation for requirement 1 can be as follows. 例如,在工作表中,需求1的实现可以如下。

function onEdit(e) {
   var sheet = SpreadsheetApp.getActiveSpreadsheet();
   var tab = sheet.getSheetByName('Item Transferring');
   sheet.setActiveSheet(tab); \\<---
   var cell = e.range;
   user_input = cell.getValue();
   if (cell.getRow() == 5 && cell.getColumn() == 2){ \\ it's probably best that you read the row and column indices of B5 and C5 from a helper tab/sheet in case you decide to move B5 and C5 later. 
      var cell2 = tab.getRange(5,3);
      cell2.setValue(cell2.getValue()+user_input);
      cell.clear({contentsOnly: true});
      SpreadsheetApp.setActiveRange(cell); \\<--- use this in conjunction with setActiveSheet. Either use both or use neither.
   }       
}

Set up an onEdit trigger for the function onEdit, and the desire effect will occur. 为函数onEdit设置一个onEdit触发器,将发生期望的效果。

Note that in your scenarios, the edits are always on single cells. 请注意,在您的方案中,编辑总是在单个单元格上。 Sometimes, edits can occur to multiple cells simultaneously. 有时,可以同时对多个单元进行编辑。 Make sure there is no ambiguity in your interface. 确保界面中没有歧义。 Also the protect function of Google Sheet may come in handy. 此外,Google表格的保护功能可能会派上用场。


Comment: 评论:

Do try to create a different interface where you are not as reliant on change per edit. 尝试创建一个不依赖于每次编辑更改的其他界面。 You can easily run into problems with synchronization. 您很容易遇到同步问题。 A final sheet that does not take a lot of inputs frequently may be fine. 不需要很多投入的最终工作表可能没问题。 It nevertheless slows down your regular activities. 但是,它会减慢您的常规活动。

As well, you should probably have a cell that displays the most recent edit. 同样,您可能应该有一个显示最新编辑的单元格。


EDIT: regarding generalizing the code and applying the above example, you can do something like this: 编辑:关于推广代码并应用上述示例,您可以执行以下操作:

function process_input(row_input,col_input,input,output,func){
  // row_input and col_input are the cell indices you are watching
  // input is the range object that the edit trigger passes in
  // output is the range object that contains the cell you want your edit to happen to
  // func contains the formula you want in the output cell

   if (input.getRow()==row_input,input.getColumn()==col_input){
      output.setValue(func(input,output));
      input.clear({contentsOnly: true});      
   }
}

As an example that can apply to the rest of your problems, your scenario 1 from earlier would require a function as below. 作为可以解决其余问题的示例,较早的方案1将需要以下功能。 Be careful that a user input can be accidentally non-numeric. 请注意,用户输入可能偶然是非数字的。

function update_add(input,output){
    if (!isNaN(input.getValue()) && isFinite(input.getValue()){
       return input.getValue()+output.getValue()
    }else{
       return output.getValue()
    }
}

You would write a simple function like the above for every kind of update you want to have. 您将为想要的每种更新编写一个类似于上面的简单函数。 To put it all together, using your scenario 1 as an example, which is to monitor B5 and update B6 using add recipe, you would do 综上所述,以场景1为例,它监视B5并使用添加配方更新B6,您可以

function onEdit(e){

...

   output = tab.getRange(5,3);
   process_input(5,2,e.range,output,update_add)

}

As mentioned earlier, it's better for you to pull the indices of the cells you are watching from a separate sheet -- as oppose to hardcoding the numbers 5, 3, 2. To implement a full solution for all of your scenarios, you can simply loop through all the cells you need to watch by update method and then loop through all possible update methods. 如前所述,最好将您正在观看的单元格的索引从另一张表中提取出来-反对对数字5、3、2进行硬编码。要为所有方案实现完整的解决方案,您只需遍历您需要通过update方法观看的所有单元,然后遍历所有可能的update方法。

You do need to implement the exact code yourself. 您确实需要自己实现确切的代码。 On Stackoverflow, we only discuss methods and how they work. 在Stackoverflow上,我们仅讨论方法及其工作方式。 We discuss using minimally self-contained examples . 我们使用最少的独立实例进行讨论。 We can't hand codes to complete people's projects. 我们无法编写代码来完成人们的项目。 That won't be a sustainable kind of interaction. 那将不是一种可持续的互动方式。

The intended take-away for you from this answer are the various Apps Script utilities linked and described with sample codes and the advices on generalization. 从该答案中可以获取的目标是各种应用程序脚本实用程序,并通过示例代码和有关泛化的建议进行了链接和描述。 That's it. 而已。 If there are questions about a utility, please read the linked documents; 如果对实用程序有疑问,请阅读链接的文档。 and if you still have questions after , open another post with a specific focus. 如果之后仍然有疑问,请打开另一个具有特定重点的帖子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google表格可以将一个单元格更改为另一个单元格 - Google Sheets change one cell from another 如何使用 If 语句将单元格值从工作表 1 转移到 Google 表格中的工作表 2? - How to use If statement to transfer a cell value from sheet1 to sheet 2 in Google Sheets? 通过单击 Google 表格中的图标,将单元格值从一个单元格自动复制到另一个工作表 - Autocopy cell value from one cell to another sheet by clicking an icon in google sheets 使用Google表格中的单元格值作为输入,使用Google App脚本将文件从一个文件夹移动到另一个文件夹 - Using a cell value in Google Sheets as an input to move a file from one folder to another using Google App Script 如何在 Google 表格上将值从一列复制到另一列? - How to copy value from one column to another on Google Sheets? 使一个单元格的值取决于Google表格中另一个单元格的值 - Make one cell's value dependent on another's in Google Sheets 如何使用一个单元格的输入来添加或减去 Google 表格中的另一个单元格? - How can the input of one cell be used to add or subtract from another cell in Google Sheets? 如何根据Google表格上的另一个单元格值更改单元格 - How to change cell based on another cell value on Google Sheets 通过Google表格中的列名将一个单元格值从一个工作表提取到另一个工作表 - Extract a cell value from one sheet to another by column name in Google Sheets Google表格根据单元格值从一张表格复制和粘贴到另一张表格 - Google Sheets Copying and Pasting from one sheet to another based on cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM