翻譯|使用教程|編輯:吉煒煒|2025-04-21 13:35:19.533|閱讀 175 次
概述:在數(shù)據(jù)分析中,一眼就能識(shí)別數(shù)值的變化對(duì)于決策至關(guān)重要。手動(dòng)設(shè)置每個(gè)單元格的格式既耗時(shí)又容易出錯(cuò)。ONLYOFFICE 宏通過(guò)動(dòng)態(tài)調(diào)整單元格顏色解決了這個(gè)問(wèn)題,讓您能夠更輕松地高效解讀復(fù)雜數(shù)據(jù)。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
ONLYOFFICE Docs,作為一款功能強(qiáng)大的在線編輯器,適用于您使用的平臺(tái)的文本文檔、電子表格、演示文稿、表單和 PDF 閱讀器。
在數(shù)據(jù)分析中,一眼就能識(shí)別數(shù)值的變化對(duì)于決策至關(guān)重要。手動(dòng)設(shè)置每個(gè)單元格的格式既耗時(shí)又容易出錯(cuò)。ONLYOFFICE 宏通過(guò)動(dòng)態(tài)調(diào)整單元格顏色解決了這個(gè)問(wèn)題,讓您能夠更輕松地高效解讀復(fù)雜數(shù)據(jù)。
var sheet = Api.GetActiveSheet(); var range = sheet.GetSelection(); var data = range.GetValue();
首先,我們分別初始化sheet、range和data 變量中的活動(dòng)工作表、選擇和范圍。
緊接著,我們檢查是否真的選中了任何數(shù)據(jù)。如果沒(méi)有選中任何數(shù)據(jù),我們將終止宏函數(shù)并顯示一條相應(yīng)的消息:“未選中任何數(shù)據(jù)”。
//We check if no data is selected, and show message if that is the case if (!data) {
console.log("No data selected");
return;
}
如果存在數(shù)據(jù),我們將繼續(xù)檢索其參數(shù)——列起始、列結(jié)束、行起始和行結(jié)束索引。
//Indexes indicating where rows and columns start and end var firstRowIndex = range.GetCells().Row;
var firstColIndex = range.GetCells().Col;
var lastRowIndex = data.length + firstRowIndex;
var lastColIndex = data[0].length + firstColIndex;
我們可以通過(guò)多種方式獲取索引,但其中一種方法如上面的代碼片段所示:
為什么是 data[0]?
由于我們不知道有多少行,并且data[0]、data[1]等每個(gè)都代表一行,因此我們唯一可以確定的是,我們的數(shù)據(jù)始終至少有一行。這意味著,只要變量dat a包含任何值, data[0]就始終有效。
接下來(lái),我們創(chuàng)建值數(shù)組并用我們選擇的數(shù)字填充它。
var values = []; //We will store number from selected data here for (var i = firstColIndex; i < lastColIndex; i++) {
for (var j = firstRowIndex; j < lastRowIndex; j++) {
//We are checking if the value is a number //If it is, we store it to values array if (!isNaN(parseFloat(sheet.GetCells(j, i).GetValue()))) {
var value = parseFloat(sheet.GetCells(j, i).GetValue());
values.push(sheet.GetCells(j, i).GetValue());
}
}
}
初始化值數(shù)組后,我們遍歷選擇中的每個(gè)單元格。
默認(rèn)情況下,每個(gè)單元格中的值都被視為字符串,因此當(dāng)我們檢查值是否為數(shù)字時(shí),我們首先需要使用parseFloat()方法將其轉(zhuǎn)換為數(shù)字。
如果單元格內(nèi)的字符串代表數(shù)字,parseFloat會(huì)將其轉(zhuǎn)換為數(shù)字。如果不是,則會(huì)將其轉(zhuǎn)換為NaN(非數(shù)字)。
isNaN(…)函數(shù)檢查括號(hào)內(nèi)的值是否為數(shù)字。如果我們?cè)?/span>isNaN(…)前面添加一個(gè)“!” ,則表示檢查該值是否為數(shù)字。
如果值是數(shù)字,我們初始化value變量并將數(shù)字存儲(chǔ)在其中。然后將該值附加到values數(shù)組中。
迭代之后,我們得到值 數(shù)組,其中包含所選單元格的所有數(shù)字。
我們需要這個(gè)數(shù)組的原因是我們可以使用Math.min()和Math.max() JavaScript 方法從選定的單元格中找到最小數(shù)字和最大數(shù)字。
//Storing minimum and maximum values from the values array var minValue = Math.min(...values);
var maxValue = Math.max(...values);
現(xiàn)在我們已經(jīng)掌握了所有必要的信息,我們可以將自定義顏色應(yīng)用到包含數(shù)字的單元格。
我們擁有所選單元格的最小數(shù)字和最大數(shù)字,因此我們可以再次遍歷這些單元格。
for (var i = firstColIndex; i < lastColIndex; i++) {
for (var j = firstRowIndex; j < lastRowIndex; j++) {
//Again we have to check if the value is a number //If it is, we create the color depending on that value //As well as minimum and maximum value from the array if (!isNaN(parseFloat(sheet.GetCells(j, i).GetValue()))) {
var value = parseFloat(sheet.GetCells(j, i).GetValue());
var ratio = (value - minValue) / (maxValue - minValue);
var red = Math.round(255 * ratio);
var green = Math.round(255 * (1 - ratio));
sheet
.GetCells(j, i)
.SetFillColor(Api.CreateColorFromRGB(red, green, 0));
//We want colors to go from green to red }
}
}
使用 RGB 系統(tǒng)創(chuàng)建顏色時(shí),我們需要紅色、綠色和藍(lán)色參數(shù)來(lái)生成任何所需的顏色。在這種情況下,顏色范圍應(yīng)從綠色到紅色,這意味著藍(lán)色參數(shù)保持為 0,而紅色和綠色參數(shù)則根據(jù)所需的色調(diào)而變化。
如果數(shù)字較小(越接近最小值),顏色就會(huì)偏綠(最小值對(duì)應(yīng)純綠色)。相反,如果數(shù)字較大,顏色就會(huì)偏紅(最大值對(duì)應(yīng)純紅色)。
為了確定數(shù)字的大小,我們使用最小-最大標(biāo)準(zhǔn)化方法來(lái)獲得 0 和 1 之間的“比率”。數(shù)字越大,比率越接近 1,而數(shù)字越小,比率越接近 0。此比率是使用minValue和maxValue變量計(jì)算的。
然后,我們可以用這個(gè)比例來(lái)確定紅色和綠色的參數(shù)。與0到1的比率不同,紅色和綠色的值都是從0到255,所以我們需要相應(yīng)地乘以比率。
由于紅色對(duì)于較大的數(shù)字來(lái)說(shuō)應(yīng)該更占主導(dǎo)地位,我們通過(guò)將比率乘以 225 并將其四舍五入到最接近的整數(shù)來(lái)計(jì)算。
對(duì)于綠色,計(jì)算方法有所不同。對(duì)于較小的數(shù)字,綠色應(yīng)該更占主導(dǎo)地位,因此我們使用1 ?比率,將其乘以 225。當(dāng)數(shù)字較大時(shí),比率趨近于 1,使得1 ?比率 更小,從而降低了綠色值。
一旦我們獲得紅色和綠色參數(shù),我們就使用Api.CreateColorFromRGB(r, g, b)來(lái)創(chuàng)建顏色并使用.SetFillColor(color)將其應(yīng)用到單元格。
(function () { var sheet = Api.GetActiveSheet(); var range = sheet.GetSelection(); var data = range.GetValue(); //We check if no data is selected, and show message if that is the case if (!data) { console.log("No data selected"); return; } //Indexes indicating where rows and columns start and end var firstRowIndex = range.GetCells().Row; var firstColIndex = range.GetCells().Col; var lastRowIndex = data.length + firstRowIndex; var lastColIndex = data[0].length + firstColIndex; console.log(firstColIndex + " " + lastColIndex); //Testing if we got the right column indexes, first should be on spot, last should be higher by 1 console.log(firstRowIndex + " " + lastRowIndex); //Testing if we got the right row indexes, first should be on spot, last should be higher by 1 var values = []; //We will store number from selected data here for (var i = firstColIndex; i < lastColIndex; i++) { for (var j = firstRowIndex; j < lastRowIndex; j++) { //We are checking if the value is a number //If it is, we store it to values array if (!isNaN(parseFloat(sheet.GetCells(j, i).GetValue()))) { var value = parseFloat(sheet.GetCells(j, i).GetValue()); values.push(sheet.GetCells(j, i).GetValue()); } } } //Storing minimum and maximum values from the values array var minValue = Math.min(...values); var maxValue = Math.max(...values); for (var i = firstColIndex; i < lastColIndex; i++) { for (var j = firstRowIndex; j < lastRowIndex; j++) { //Again we have to check if the value is a number //If it is, we create the color depending on that value //As well as minimum and maximum value from the array if (!isNaN(parseFloat(sheet.GetCells(j, i).GetValue()))) { var value = parseFloat(sheet.GetCells(j, i).GetValue()); var ratio = (value - minValue) / (maxValue - minValue); var red = Math.round(255 * ratio); var green = Math.round(255 * (1 - ratio)); sheet .GetCells(j, i) .SetFillColor(Api.CreateColorFromRGB(red, green, 0)); //We want colors to go from green to red } } } })();
讓我們運(yùn)行宏并看看它是如何工作的!
在我們運(yùn)行宏之前:
運(yùn)行宏后:
現(xiàn)在,您可以輕松在電子表格中可視化數(shù)據(jù)分布,使分析更加直觀。無(wú)論您使用的是 ONLYOFFICE 桌面版還是網(wǎng)頁(yè)版,此宏都能無(wú)縫集成到您的工作流程中。
關(guān)于慧都科技:
慧都科技是一家行業(yè)數(shù)字化解決方案公司,長(zhǎng)期專注于軟件、油氣與制造行業(yè)。公司基于深入的業(yè)務(wù)理解與管理洞察,以系統(tǒng)化的業(yè)務(wù)建模驅(qū)動(dòng)技術(shù)落地,幫助企業(yè)實(shí)現(xiàn)智能化運(yùn)營(yíng)與長(zhǎng)期競(jìng)爭(zhēng)優(yōu)勢(shì)。在軟件工程領(lǐng)域,我們提供開(kāi)發(fā)控件、研發(fā)管理、代碼開(kāi)發(fā)、部署運(yùn)維等軟件開(kāi)發(fā)全鏈路所需的產(chǎn)品,提供正版授權(quán)采購(gòu)、技術(shù)選型、個(gè)性化維保等服務(wù),幫助客戶實(shí)現(xiàn)技術(shù)合規(guī)、降本增效與風(fēng)險(xiǎn)可控。慧都科技ONLYOFFICE在中國(guó)的官方授權(quán)代理商,提供ONLYOFFICE系列產(chǎn)品免費(fèi)試用,咨詢,正版銷(xiāo)售等于一體的專業(yè)化服務(wù)。ONLYOFFICE是文檔在線編輯領(lǐng)域的優(yōu)秀產(chǎn)品,幫助企業(yè)實(shí)現(xiàn)高效的文檔協(xié)同處理。
下載|體驗(yàn)更多ONLYOFFICE產(chǎn)品,請(qǐng)咨詢,或撥打產(chǎn)品熱線:023-68661681
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)