Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 如何使一个单元格根据另一个单元格移动?_Sorting_Google Apps Script_Google Sheets_Google Sheets Formula_Google Sheets Filter View - Fatal编程技术网

Sorting 如何使一个单元格根据另一个单元格移动?

Sorting 如何使一个单元格根据另一个单元格移动?,sorting,google-apps-script,google-sheets,google-sheets-formula,google-sheets-filter-view,Sorting,Google Apps Script,Google Sheets,Google Sheets Formula,Google Sheets Filter View,我有一个系统,其中有两列单元格,在这些列中有名称和名称数据 我不喜欢分享我的电子表格,所以这里有一个例子 Food \ Quantity ------ -------- Pasta | 4 Lemons | 4 Chicken | 4 但是现在,代码被发送到数量,数字也改变了 Pasta is now: 2 Lemons is still: 4 Chicken is now: 1 现在在食物列(E列)中,每当我对数量列排序时,我希望它移动,但我不希望它丢失正确的数字 因此,只要4个柠

我有一个系统,其中有两列单元格,在这些列中有名称和名称数据

我不喜欢分享我的电子表格,所以这里有一个例子

Food \ Quantity
------ --------
Pasta  | 4

Lemons | 4

Chicken | 4

但是现在,代码被发送到数量,数字也改变了

Pasta is now: 2
Lemons is still: 4
Chicken is now: 1
现在在食物列(E列)中,每当我对数量列排序时,我希望它移动,但我不希望它丢失正确的数字

因此,只要4个柠檬排到列表的顶部,柠檬就会移动。意大利面也一样,但在1格以下。鸡又是最后一个了,就好像食物细胞基本上都在跟随它们的数量细胞

所以现在它会自动更新到这个

Food \ Quantity
------ --------
Lemons  | 4

Pasta | 2

Chicken | 1

我希望这一切都能在我不手动触摸的情况下发生


那么这需要什么公式呢?我是新来的,所以像4岁一样给我写信。

选择如下范围:

然后做:

然后单击这3行,并根据需要对其进行排序:


脚本:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");     //SHEET NAME
var range = sheet.getRange("A2:Z");          //RANGE TO BE SORTED

function onEdit(e)  {                        //NUMBER OF COLUMN(S) TO BE SORTED
  range.sort([{column: 4, ascending: false}, {column: 1, ascending: true}]);
}

@粗体滚轴答案已使用脚本解决方案更新