[英]How to Print the names of the sheets and the Google ID
I am trying to get print the names of all the sheets in a Google Spreadsheet in one column and their respective IDs (part of the URL) in another column.我试图打印一列中 Google 电子表格中所有工作表的名称,并在另一列中打印它们各自的 ID(URL 的一部分)。 Here is my script so far
到目前为止,这是我的脚本
function getSchedule() {
var ss=SpreadsheetApp.openById("Sheet_ID");
var sh=ss.getActiveSheet();
var allSheets=ss.getSheets();
var ids=[];
for(var i=0;i<allSheets.length;i++)
{
ids[allSheets[i].getName()]=allSheets[i].getSheetId();
}
var invoicess = SpreadsheetApp.getActiveSpreadsheet();
var schedule = invoicess.getSheetByName("Schedule");
schedule.getRange("A1:B").clear();
var headers = [["Sheet Name", "ID"]];
schedule.getRange("A1:B1").setValues(headers);
for(var d = 1; d=allSheets.length;d++){
schedule.getRange(d+1,1).setValue(allSheets[d]);}
}
So I believe that allSheets should be an array of all the sheet names and ids is an array of the IDs, but the last for loop that I believe should print the values of the allSheets array does not print anything.所以我相信 allSheets 应该是所有工作表名称的数组,而 ids 是一个 ID 数组,但我认为应该打印 allSheets 数组值的最后一个 for 循环不打印任何内容。 I would like to have it print starting in cell
A2
.我想让它从单元格
A2
开始打印。 I do not have a loop for the IDs yet.我还没有 ID 循环。 Can anyone tell me why nothing is printing in the cells?
谁能告诉我为什么在单元格中什么也没有打印?
ids[allSheets[i].getName()]=allSheets[i].getSheetId();
ids[allSheets[i].getName()]=allSheets[i].getSheetId();
setValues
instead of setValue
.setValues
而不是setValue
。setValues
to add the header in your file when you can simply add it to the original array in which the data is going to be appended.setValues
在文件中添加标题,而您只需将其添加到将要附加数据的原始数组中即可。 You don't need for loops to append values with setValues
.setValues
。function getSchedule() {
const ss=SpreadsheetApp.openById("Sheet_ID");
const invoicess = SpreadsheetApp.getActiveSpreadsheet();
const schedule = invoicess.getSheetByName("Schedule");
const allSheets = ss.getSheets();
const ids = [["Sheet Name", "ID"]];
allSheets.forEach(sh=>{
ids.push([sh.getName(),sh.getSheetId()]);
});
schedule.getRange("A1:B").clear();
schedule.getRange(1,1,ids.length,ids[0].length).setValues(ids);
}
Instead of this ids[allSheets[i].getName()]=allSheets[i].getSheetId();
而不是这个
ids[allSheets[i].getName()]=allSheets[i].getSheetId();
try this: ids.push([allSheets[i].getName(),allSheets[i].getSheetId()])
试试这个:
ids.push([allSheets[i].getName(),allSheets[i].getSheetId()])
and use setValues(ids) at the end.并在最后使用 setValues(ids) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.