[英]Scroll a row to the top of the screen based on cell value: Google Sheet Script
[英]Google Sheet script that reads cell “a” and writes to cell “b” based off cell “a” value
大家早上好,
我需要為Google表格編寫腳本,以檢查該行中的單元格是否包含值,該腳本會將值寫入該行中的列。 例如,如果第2行第2列的單元格中的單元格的值不是“”,則在第2行第C列的單元格中寫“新問題”。到目前為止,我有以下內容:
// Purpose: To populate cell in Column G if another in row cell contains
// something other than null, or ""
function formatSpreadsheet() {
var sheet = SpreadsheetApp.getActive();
var data = sheet.getRange("C3:1000").getValues();
if (data !== "") {
sheet.getRange("G3:G1000").setValue("New Issue");
}
}
這樣做的問題是,如果在C3-C1000中找到一個值,則會將“新問題”寫入G3-G1000范圍內的所有單元格。 我需要它只將其寫入相應行的位置。 因此,如果C4具有值,則G4應設置為“新發行”,如果C5具有值,則G5應設置為“新發行”,依此類推。
希望這是有道理的,我可以嘗試盡可能地清除它。 我不知道“ IF語句”在這種情況下是否有幫助,或者每個循環是否需要a。 我可悲的是我不知道該如何使用。 謝謝您的幫助!
針對此請求的Apps腳本解決方案需要檢查每個數組索引,並寫入其他范圍內的相應索引。 為了最大化速度,我們希望在可能的情況下使用批處理操作getValues()
和setValues
。 一種可能的解決方案如下所示:
function setNewIssueLabels() {
const sourceRangeA1 = "C3:C1000", destStart = "G3";
const sheet = SpreadsheetApp.getActive().getActiveSheet(),
source = sheet.getRange(sourceRangeA1).getValues();
if (!source.length || source[0].length !== 1)
throw new Error("Expected single-column source range with at least 1 row");
// Include the original row index in the array.
const inputs = source.map(function (row, index) {
row.unshift(index);
return row;
})
// Keep rows where the 2nd index (the original content) is not a null string.
.filter(function (iRow) {
return iRow[1] !== "";
});
var dest = sheet.getRange(destStart);
if (dest.getNumRows() < source.length)
dest = dest.offset(0, 0, source.length, 1)
const outputs = dest.getValues();
// For every row in 'inputs' (read: every row in the source range that
// has a value), do stuff.
inputs.forEach(function (iRow) {
var originalIndex = iRow[0];
// var originalContent = iRow[1];
// var isActualNewIssue = outputs[originalIndex] === "";
// if (isActualNewIssue) {
outputs[originalIndex] = "New Issue";
// } else {
// foo();
// }
});
/**
* Overwrite the contents of the destination range with the 'outputs' values,
* which will be whatever was previously there, and possibly additional
* cells with "new Issue"
*/
dest.setValues(output);
}
我添加並注釋了一些邏輯,以確定該值是否在該函數的上一次調用中存在,或者它是否是真正的新值。
事實證明,一個簡單的公式無需腳本即可自行完成此操作。
只需要“ = ArrayFormula(if(len(C2:C),” New Issue“,))”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.