[英]Google Sheets - Time Stamps and clearContent
我正在嘗試創建一個谷歌表格系統,供人們用來幫助跟蹤他們的日常活動。 我們目前每天都在手寫這個。 我最初在 excel 上創建了一個類似的,但我需要它可以被多人在多個系統上使用,所以我將它移動到谷歌表。
我目前無法弄清楚的是,當第 1 列中的信息被刪除(在 A5 和 A55 之間)時,如何在行上正確使用 clearContents。
簡而言之,當單元格 A6 被刪除時,我想在第 6 行的其余部分使用 clearConents,但是當在 A6 中輸入數據時,它會將時間填充到第 6 行的不同單元格中。
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var activeCell = sheet.getActiveCell();
var col = activeCell.getColumn();
var row = activeCell.getRow();
//Start Time Function
if (col == 1 && isBlank){ // These first two rows are what I cannot figure out.
sheet.getRange(row).clearContent();
}
else if (col == 1){
sheet.getRange(row, col+11).setValue(new Date()).setNumberFormat('hh:mm:ss');
sheet.getRange(row,col+13).setValue(new Date()).setNumberFormat('hhmm');
sheet.getRange(row, col+5).setValue(new Date()).setNumberFormat('hhmm');
sheet.getRange(row, col+3).setValue(new Date()).setNumberFormat();
}
//End Time Function
if (col == 8) {
sheet.getRange(row, col+5).setValue(new Date()).setNumberFormat('hh:mm:ss');
sheet.getRange(row, col+7).setValue(new Date()).setNumberFormat('hhmm');
sheet.getRange(row, col-1).setValue(new Date()).setNumberFormat('hhmm');
}
}
修改
根據其他答案,您的工作代碼可以通過以下修改進行優化:
onEdit
觸發器事件對象有一個range
屬性,您可以使用它來代替getActiveCell
( onEdit
,建議使用getCurrentCell
代替)。setValues
、 setNumberFormats
)並對內存中的對象進行修改。 調用 I/O(輸入/輸出)方法(如循環或重復調用getRange
或setValue
)很慢。===
類型安全比較。 為什么? 看看這個: console.log( 0 == "" ); console.log( [] == "" ); console.log( 0 === "" ); console.log( [] === "" );
應用所有修改后,您的腳本將變為以下內容:
function onEdit({
source,
range: currRng
}) {
var sheet = source.getActiveSheet();
var col = currRng.getColumn();
var row = currRng.getRow();
var val = currRng.getValue();
const maxRows = sheet.getMaxRows();
const maxCols = sheet.getMaxColumns();
const range = sheet.getRange(1, 1, maxRows, maxCols);
const values = range.getValues();
const formats = range.getNumberFormats();
const now = new Date();
const hhmmssFormat = "hh:mm:ss";
const hhmmFormat = "hhmm";
const colIdx = col - 1;
const rowIdx = row - 1;
//Start Time Function
if (col === 1 && row >= 5 && row <= 55 && val === "") {
sheet.getRange(row, 1, 1, maxCols).clearContent();
} else if (col === 1) {
const newValues = values.map((row, ri) => {
if (ri === rowIdx) {
row[colIdx + 11] = now;
row[colIdx + 13] = now;
row[colIdx + 5] = now;
row[colIdx + 3] = now;
}
return row;
});
const newFormats = formats.map((row, ri) => {
if (ri === rowIdx) {
row[colIdx + 11] = hhmmssFormat;
row[colIdx + 13] = hhmmFormat;
row[colIdx + 5] = hhmmFormat;
row[colIdx + 3] = hhmmFormat;
}
return row;
});
range.setValues(newValues);
range.setNumberFormats(newFormats);
console.log(range.getValues()[14]);
console.log(range.getNumberFormats()[14]);
}
//End Time Function
if (col === 8) {
const newValues = values.map(row => {
row[colIdx + 5] = now;
row[colIdx + 7] = now;
row[colIdx - 1] = now;
return row;
});
const newFormats = formats.map(row => {
row[colIdx + 5] = hhmmssFormat;
row[colIdx + 7] = hhmmFormat;
row[colIdx - 1] = hhmmFormat;
return row;
});
range.setValues(newValues);
range.setNumberFormats(newFormats);
}
}
您必須啟用V8 運行時才能使上述工作正常運行。
使用模擬的工作片段:
function Range({ sheet, grid, row = 1, column = 1, numRows = 1, numColumns = 1 }) { const intRow = row - 1; const intCol = column - 1; /** @type {function (any[][], function)} */ const sliceMap = (g, row, col, rows, cols, mapper) => g.slice(row, row + rows).map( (r) => r.slice(intCol, col + cols).map(mapper) ); /** @type {function (any[][], any[][], number, number, string)} */ const sliceUpdate = (g, ug, row, col, prop) => g.forEach( (r, ri) => r.forEach((c, ci) => { if (ri < row || ci < col) { return; } const arow = ri - row; const acol = ci - col; g[ri][ci][prop] = ug[arow][acol]; }) ); return { activate() { sheet.setActiveRange(this); return this; }, clearContent() { grid.forEach((r, ri) => r.forEach((c, ci) => { ri >= intRow && ci >= intCol && (grid[ri][ci].value = ""); })); return this; }, getCell(row, column) { return Range({ sheet, grid, row: row + intRow, column: column + intCol }); }, getColumn() { return column; }, getFormulas() { return sliceMap(grid, numRows, numColumns, ({ formula }) => formula); }, setFormulas(formulas) { sliceUpdate(grid, formulas, intRow, intCol, "formula"); return this; }, getNumberFormats() { return sliceMap(grid, intRow, intCol, numRows, numColumns, ({ format }) => format); }, setNumberFormats(numberFormats) { sliceUpdate(grid, numberFormats, intRow, intCol, "format"); return this; }, getRow() { return row; }, getValue() { const [val] = sliceMap(grid, intRow, intCol, numRows, numColumns, ({ value }) => value); return val[0]; }, getValues() { return sliceMap(grid, intRow, intCol, numRows, numColumns, ({ value }) => value); }, setValues(values) { sliceUpdate(grid, values, intRow, intCol, "value"); return this; } }; } function Sheet(spreadsheet) { /** @type {{ value }[][]} */ const grid = []; let sheetName = "Sheet1"; let active = null; return { activate() { spreadsheet.setActiveSheet(this); return this; }, getCurrentCell() { return active && active.getCell( active.getRow(), active.getColumn() ); }, getDataRange() { return Range({ grid, sheet: this }); }, getLastRow() { return grid.findIndex(row => row.some(({ value }) => value !== "")) + 1 || 1; }, getLastColumn() { const lasts = grid.map(row => row.reverse().findIndex(({ value }) => value !== "") + 1); return Math.max(...lasts) || 1; }, getMaxColumns() { return grid[0].length; }, getMaxRows() { return grid.length; }, getRange(row, column, numRows = 1, numColumns = 1) { return Range({ grid, sheet: this, row, column, numRows, numColumns }); }, getSheetName() { return sheetName; }, insertColumns(columnIndex, numColumns) { grid.forEach(row => { const cols = new Array(numColumns).fill("") .map(() => ({ value: "", formula: "", format: "" })); row.splice(columnIndex, 0, ...cols); }); return this; }, insertRows(rowIndex, numRows) { const rows = new Array(numRows).fill("") .map(() => [{ value: "", formula: "", format: "" }]); grid.splice(rowIndex, 0, ...rows); return this; }, setActiveRange(range) { active = range; return this; }, setName(name) { sheetName = name; return this; } }; } function Spreadsheet() { const defaultSheet = Sheet(this); const sheets = [defaultSheet]; let active = defaultSheet; return { getSheets() { return sheets; }, getActiveSheet() { return active; }, insertSheet(sheetIndex = 0) { const sheet = Sheet(this); sheets.splice(sheetIndex, 0, sheet); return sheet.activate(); }, setActiveSheet(sheet) { active = sheet; return sheet; } }; } var SpreadsheetApp = { getActiveSpreadsheet() { return Spreadsheet(); } }; function onEdit({ source, range: currRng }) { var sheet = source.getActiveSheet(); var col = currRng.getColumn(); var row = currRng.getRow(); var val = currRng.getValue(); const maxRows = sheet.getMaxRows(); const maxCols = sheet.getMaxColumns(); const range = sheet.getRange(1, 1, maxRows, maxCols); const values = range.getValues(); const formats = range.getNumberFormats(); const now = new Date(); const hhmmssFormat = "hh:mm:ss"; const hhmmFormat = "hhmm"; const colIdx = col - 1; const rowIdx = row - 1; //Start Time Function if (col === 1 && row >= 5 && row <= 55 && val === "") { sheet.getRange(row, 1, 1, maxCols).clearContent(); } else if (col === 1) { const newValues = values.map((row, ri) => { if (ri === rowIdx) { row[colIdx + 11] = now; row[colIdx + 13] = now; row[colIdx + 5] = now; row[colIdx + 3] = now; } return row; }); const newFormats = formats.map((row, ri) => { if (ri === rowIdx) { row[colIdx + 11] = hhmmssFormat; row[colIdx + 13] = hhmmFormat; row[colIdx + 5] = hhmmFormat; row[colIdx + 3] = hhmmFormat; } return row; }); range.setValues(newValues); range.setNumberFormats(newFormats); console.log(range.getValues()[14]); console.log(range.getNumberFormats()[14]); } //End Time Function if (col === 8) { const newValues = values.map(row => { row[colIdx + 5] = now; row[colIdx + 7] = now; row[colIdx - 1] = now; return row; }); const newFormats = formats.map(row => { row[colIdx + 5] = hhmmssFormat; row[colIdx + 7] = hhmmFormat; row[colIdx - 1] = hhmmFormat; return row; }); range.setValues(newValues); range.setNumberFormats(newFormats); } } //Test: (() => { const ss = SpreadsheetApp.getActiveSpreadsheet(); const sh = ss.getActiveSheet(); sh.insertRows(0, 55); sh.insertColumns(0, 13); const rng = sh.getRange(1, 1, sh.getMaxRows(), sh.getMaxColumns()); rng.activate(); const vals = rng.getValues(); vals[14][0] = "val"; rng.setValues(vals); onEdit({ source: ss, range: sh.getRange(15, 1) }); })();
你很親近。 本質上,您需要像這樣修改第一個 if 條件:
if (col == 1 && row>=5 && row<=55 && sheet.getRange(row,col).getValue() == "")
col == 1
:必須編輯 A 列中的單元格row >=5
和<=55
:必須編輯 A5 和 A55 之間的單元格。value == ""
:您清除了它的值。如果上述條件評估為真,則清除整行:
sheet.getRange(row,1,1,sheet.getMaxColumns()).clearContent();
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var activeCell = sheet.getActiveCell();
var col = activeCell.getColumn();
var row = activeCell.getRow();
//Start Time Function
if (col == 1 && row>=5 && row<=55 && sheet.getRange(row,col).getValue() == ""){
sheet.getRange(row,1,1,sheet.getMaxColumns()).clearContent();
}
else if (col == 1){
sheet.getRange(row, col+11).setValue(new Date()).setNumberFormat('hh:mm:ss');
sheet.getRange(row,col+13).setValue(new Date()).setNumberFormat('hhmm');
sheet.getRange(row, col+5).setValue(new Date()).setNumberFormat('hhmm');
sheet.getRange(row, col+3).setValue(new Date()).setNumberFormat('hhmm');
}
//End Time Function
if (col == 8) {
sheet.getRange(row, col+5).setValue(new Date()).setNumberFormat('hh:mm:ss');
sheet.getRange(row, col+7).setValue(new Date()).setNumberFormat('hhmm');
sheet.getRange(row, col-1).setValue(new Date()).setNumberFormat('hhmm');
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.