我對使用 Google Apps Script 很陌生。在制作基于所有列的列標題名稱創建命名范圍的動態公式時,我需要一些幫助。命名范圍的范圍將是 [column]2:[column] - 即該列中除第 1 行之外的所有值。我需要命名范圍以制作相關資料驗證串列。
在某些情況下,此腳本以匯入范圍開始,我復制/粘貼沒有格式化,因此我可以設定這些命名范圍。
//this function pulls header values for other scripts below
function getHeaderValues() {
return SpreadsheetApp.getActiveSheet().getRange('1:1').getValues();
}
...
function groupnamedranges() {
const spreadsheet = SpreadsheetApp.getActive();
const col = spreadsheet.getRange('A:N').getColumn();
//this is to convert importrange
spreadsheet.getRange('A:N').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
//this is to make named ranges for all columns based on header name
spreadsheet.setNamedRange([getHeaderValues()], spreadsheet.getRange(2, col));
};
我離我有多遠?
(PS 我所有的標題都是命名范圍的可接受名稱 - 已經對此進行了檢查)。
uj5u.com熱心網友回復:
從列標題生成命名范圍
function headerNamedRanges() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const hA = sh.getRange(1, 1, 1, sh.getLastColumn()).getDisplayValues()[0];
hA.forEach((h, i) => {
let rg = sh.getRange(2, i 1, sh.getLastRow() - 1, 1).activate();
Logger.log(h);
ss.setNamedRange(h, ss.getActiveRange());
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/504971.html