簡體   English   中英

更新列值谷歌表 python

[英]Update column values google sheet python

我是谷歌表 python 的新手,我想知道有任何方法可以將列號(55)或標題(CM)作為參數並為我提供更新的列值。

或者

任何返回給定列的新輸入值或更新值的代碼。

例子:

在此處輸入圖像描述

CM 列中有 6 個可用值,當七個值出現在同一列中時,所以我想要一個方法來返回 CM 列中出現的確切值。

如果有一種方法或任何方法可以為我提供更新的或新的列值,那么它對我很有幫助。

我將運行一個無限循環並將該方法放入其中,當任何輸入出現在任何 position 的給定列中時,該方法將返回我的值。

這是我建議的方法:

1.) 每當用戶編輯 CM 列時,我都會使用 Apps 腳本創建觸發器。 當用戶編輯 CM 列(更新值或添加新值)時,腳本將顯示從 CO 到 CR(第 2 行)的以下信息:

  • CO - 類型(更新或新 - 更新價值或新的附加值)

  • CP - Row(CM 列中已編輯的行號)

  • CQ - 舊值(對於“更新”值,它將顯示舊值。對於“新”值,它將在單元格中顯示一個空值)

  • CR - 新值(對於“更新”值,它將顯示更新的值。對於“新”值,它將在單元格中顯示新添加的值)

2.) 使用 python 腳本通過 Google Sheet API 監控這些值的變化。

3.) 谷歌表格 API 不支持 onEdit。 它不監視單元格中的變化。

這是我用來實現目標的腳本:

function onEdit(e) {
  //Get edited cell
  var range = e.range;
  //Ignore the event if editing the other column but not the column CM
  //Ignore the event if deleting the cell value
  if (range.getA1Notation().indexOf("CM") != 0 || (e.oldValue == null && e.value == null)) {
    return;
  }
  
  //Get the current active sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  //Get to range "CO2:CR2". The result will show in this range
  //CO2 is Type (New or Update)
  //CP2 is Row. Which row in the column is updated
  //CQ2 is the old value. For the type 'Update', it will show the old value. For the type 'New', it will show empty value
  //CR2 is the new value. It will show the updated value for the type 'Update' or it will show the new added value for the type 'New'
  var resultRange = sheet.getRange("CO2:CR2");
  
  //Show the result
  resultRange.setValues([[e.oldValue == null ? "New":"Update", range.getRow(), e.oldValue, e.value]])  
}

這是我的電子表格中的示例結果:

在此處輸入圖像描述

如您所見,我將 CM6 的值從 60 更改為 25,結果顯示在 CQ2(舊值)和 CR2(新值)中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM