有沒有辦法在谷歌表格的單元格中檢測前導單引號?
單元格 A1 設定為:
'005

我希望有條件地格式化包含前導'/的單元格CHAR(39)并將背景涂成藍色
uj5u.com熱心網友回復:
根據此
用于=FIND("'",B1)測驗
uj5u.com熱心網友回復:
我相信你的目標如下。
- 您想要將單元格值與
'頂部字符的單元格的背景顏色更改為藍色。 - 您的目標包括使用 Google Apps 腳本。
根據我的經驗,我認為在這種情況下,可能沒有直接'的方法來檢索頂級字符。不幸的是,即使使用 Sheets API,'也無法檢索。所以,在這個答案中,我想提出一個解決方法。此解決方法的流程如下。
- 將要使用的作業表轉換為 XLSX 格式。
'通過決議 XLSX 資料,檢測具有頂部字符的單元格值的單元格。- 將檢測到的單元格的背景顏色更改為藍色。
在 Microsoft Docs 中,詳細規范以 Open XML 形式發布。因此,在這個答案中,通過分析 XLSX 資料,'可以僅使用 Google Apps Script 的本機方法來檢索具有頂部字符的單元格的單元格坐標。當這個流程反映在 Google Apps Script 中時,它變成如下。
示例腳本:
請將以下腳本復制并粘貼到您要使用的 Google 電子表格的腳本編輯器中,然后保存該腳本。并且,請設定作業表名稱。
function myFunction() {
const sheetName = "Sheet1"; // Please set the sheet name.
// Convert Google Spreadsheet to XLSX format.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const srcSheet = ss.getSheetByName(sheetName);
const url = `https://docs.google.com/spreadsheets/export?exportFormat=xlsx&id=${ss.getId()}&gid=${srcSheet.getSheetId()}`;
const res = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " ScriptApp.getOAuthToken() } });
// Retrieve the data from XLSX data.
const blobs = Utilities.unzip(res.getBlob().setContentType(MimeType.ZIP));
const { sheet, style } = blobs.reduce((o, b) => {
const name = b.getName();
if (name == "xl/styles.xml") {
o.style = b.getDataAsString();
} else if (name == "xl/worksheets/sheet1.xml") {
o.sheet = b.getDataAsString();
}
return o;
}, {});
// Detect the cells including the single quote at the top character.
const styler = XmlService.parse(style).getRootElement();
const quotePrefix = styler.getChild("cellXfs", styler.getNamespace()).getChildren().map(e => e.getAttribute("quotePrefix") ? true : false);
const sr = XmlService.parse(sheet).getRootElement();
const ranges = sr.getChild("sheetData", sr.getNamespace()).getChildren().reduce((ar, r, i) => {
r.getChildren().forEach((c, j) => {
const v = Number(c.getAttribute("s").getValue());
const columnIndexToLetter_ = index => (a = Math.floor(index / 26)) >= 0 ? columnIndexToLetter_(a - 1) String.fromCharCode(65 (index % 26)) : ""; // Ref: https://stackoverflow.com/a/53678158
if (quotePrefix[v]) ar.push(`${columnIndexToLetter_(j)}${i 1}`);
});
return ar;
}, []);
// Change the background color of detected cells.
srcSheet.getRangeList(ranges).setBackground("blue");
}
測驗:
運行此腳本時,作為示例情況,將獲得以下情況。在此示例中,在輸入情況下,單元格“A1:A3”的值為'500。并且,單元格“B1:B3”的值為500。運行此腳本時,“A1:A3”的背景顏色會發生變化。

參考:
- 了解 Open XML 檔案格式
- XML 服務
uj5u.com熱心網友回復:
使用公式或自定義函式似乎是不可能的。但是,您可以使用TextFinder“查找和替換”功能。使用正則運算式的起始錨點^,我們將單元格替換為公式并回傳文本。區別因素是使用此方法無法將具有單引號前綴的單元格轉換為公式。
'005=>'=005(不是公式)abc=>=abc(是一個公式)=1 5=>==1 5(仍然是一個公式)
/**
* @OnlyCurrentDoc
* @description Detects single quotes in given range and colors them
* @author TheMaster
* @link https://stackoverflow.com/a/73621052
*/
function detectSingleQuotePrefix(range = 'Sheet1!A1:A10', color = 'blue') {
const dataRange = SpreadsheetApp.getActive().getRange(range),
txtFinder = (find, replace, matchFormula) =>
dataRange
.createTextFinder(find)
.matchFormulaText(matchFormula)
.useRegularExpression(true)
.replaceAllWith(replace),
isEmpty = (e) =>
Array.isArray(e) ? e.map(isEmpty) : e === '' ? color : null;
txtFinder('^. ', '=????$0', false);
const dividedColors = dataRange.getFormulas().map(isEmpty);
txtFinder('^=????', '', false);
txtFinder('^=????', '', true);
dataRange.setBackgrounds(dividedColors);
console.log(dividedColors);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/504957.html
標籤:谷歌应用脚本 谷歌表格 谷歌表格公式 公式 电子表格
上一篇:如何在C#Windows應用程式中從富文本框中獲取和保存特定記錄
下一篇:基于表中值的條件格式
