![](/img/trans.png)
[英]Getting all column values from google sheet using Gspread and Python
[英]Update column values google sheet python
這是我建議的方法:
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.