Google Apps 脚本 - 重命名具有特定单元格值的所有工作表的功能
Google Apps Script - FUNCTION renaming all sheets with specific cell value
我正在使用此函数根据 B4 中的单元格值重命名活动 sheet - 但我想为整个跨页 sheet、每个 sheet 重命名应该在 B4 中重命名为它自己的特定单元格值。
谁能告诉我如何用 FOR 循环实现这个,例如?!谢谢!
function RenameSheetsM() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet(); // active Sheet = Tab
var cell = sheet.getRange("B4");// cell B4 = Name
var value = cell.getValue();//Added
sheet.setName(value);//Sheet name changed
}
可以是这样的:
function main() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
for (let sheet of sheets) {
RenameSheetsM(sheet);
}
}
function RenameSheetsM(sheet) {
var cell = sheet.getRange("B4");// cell B4 = Name
var value = cell.getValue();//Added
sheet.setName(value);//Sheet name changed
}
但我更喜欢循环 while()
,它看起来很整洁:
function main() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
while (sheets.length) {
var sheet = sheets.shift();
var cell = sheet.getRange("B4");
var value = cell.getValue();
sheet.setName(value);
}
}
function RenameSheetsM() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
for (i=0; i<sheets.length; i++) {
var value = sheets[i].getRange("B4").getValue();
if(sheets[i].getName() != value){
sheets[i].setName(value);
}
}
}
重命名电子表格中的所有工作表
function renameSheets() {
SpreadsheetApp.getActive().getSheets().forEach(sh => sh.setName(sh.getRange('B4').getDisplayValue()));
}
我正在使用此函数根据 B4 中的单元格值重命名活动 sheet - 但我想为整个跨页 sheet、每个 sheet 重命名应该在 B4 中重命名为它自己的特定单元格值。 谁能告诉我如何用 FOR 循环实现这个,例如?!谢谢!
function RenameSheetsM() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet(); // active Sheet = Tab
var cell = sheet.getRange("B4");// cell B4 = Name
var value = cell.getValue();//Added
sheet.setName(value);//Sheet name changed
}
可以是这样的:
function main() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
for (let sheet of sheets) {
RenameSheetsM(sheet);
}
}
function RenameSheetsM(sheet) {
var cell = sheet.getRange("B4");// cell B4 = Name
var value = cell.getValue();//Added
sheet.setName(value);//Sheet name changed
}
但我更喜欢循环 while()
,它看起来很整洁:
function main() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
while (sheets.length) {
var sheet = sheets.shift();
var cell = sheet.getRange("B4");
var value = cell.getValue();
sheet.setName(value);
}
}
function RenameSheetsM() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
for (i=0; i<sheets.length; i++) {
var value = sheets[i].getRange("B4").getValue();
if(sheets[i].getName() != value){
sheets[i].setName(value);
}
}
}
重命名电子表格中的所有工作表
function renameSheets() {
SpreadsheetApp.getActive().getSheets().forEach(sh => sh.setName(sh.getRange('B4').getDisplayValue()));
}