简体   繁体   English

如何打印工作表的名称和 Google ID

[英]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?谁能告诉我为什么在单元格中什么也没有打印?

Issues:问题:

  • The following line is not going to add new values to the array: ids[allSheets[i].getName()]=allSheets[i].getSheetId();以下行不会向数组添加新值: ids[allSheets[i].getName()]=allSheets[i].getSheetId();
  • As a result, data are not added to the array and this is the reason you are not seeing data in your sheet.因此,数据不会添加到数组中,这就是您没有在工作表中看到数据的原因。 Instead you should use push() to add elements (rows) to the array.相反,您应该使用push()将元素(行)添加到数组中。
  • Also since you are pasting a 2D array of values, you need to use setValues instead of setValue .此外,由于您要粘贴值的二维数组,因此您需要使用setValues而不是setValue
  • Your code uses unnecessary lines of code.您的代码使用了不必要的代码行。 For example you use 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 .您不需要 for 循环来使用setValues
  • I improved your code by removing redundant calls and made it more JavaScript friendly.我通过删除多余的调用改进了您的代码,使其对 JavaScript 更加友好。

Improved Solution:改进的解决方案:

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.

相关问题 将 Firestore 数组打印到 Google 表格 - Print Firestore array to Google Sheets 从 Google 表格打印图表 - Print chart from Google Sheets 如何使用 Google Apps 脚本将 Google Drive 文件名记录到 Google Sheets - How to log Google Drive File Names to Google Sheets using Google Apps Script 从Google电子表格文档中获取工作表名称 - Get sheets names from google spreadsheets docs 如何在 Google Form 或 Google Sheets 上使用 GAS 根据两个 ID 限制 google forms 提交 - How to restrict google forms submission based on two ID using GAS on Google Form or Google Sheets 如何使用Apps脚本将对Google Analytics API的多次调用打印到Google Sheets - How to print multiple calls to Google Analytics API to Google Sheets with Apps Script 水平打印Google表格中的图表 - Print a chart from Google Sheets horizontally Facebook和谷歌如何随机化html选择器ID和类名? - How does Facebook and google randomize the html selector id and class names? 如何使用脚本将工作表名称与数组元素进行比较并在 Google 工作表上执行任务? - How to compare Sheet Names with Array elements and execute tasks on Google Sheets using script? 如何将多元素 JSON object 不同级别的值打印到 Google 表格中? - How do I print values from different levels of a multi-element JSON object into Google Sheets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM