简体   繁体   English

在 Google Sheets Apps Script 中,如何检查工作表是否存在以及是否不添加工作表

[英]In Google Sheets Apps Script, how to check if a sheet exists, and if it doesn't add one

I am creating a spreadsheet to sort events based on the date inputted.我正在创建一个电子表格来根据输入的日期对事件进行排序。 I have successfully gotten the program to add a new sheet with the contents of the event that I want to copy over, but I have not been able to add to the script to allow for two options.我已经成功地让程序添加了一个包含我想要复制的事件内容的新工作表,但是我无法添加到脚本中以允许两个选项。 My goal is for it to go something like this:我的目标是 go 是这样的:

Option 1: The sheet already exists.选项 1:工作表已存在。 A2:D2 cells will be copied from the template sheet to the sheet that is titled the same date as is in cell A2. A2:D2 单元格将从模板工作表复制到标题与单元格 A2 中相同日期的工作表。 . . A2:D2 will be cleared from the template sheet as to leave a blank canvas. A2:D2 将从模板表中清除,留下空白 canvas。

Option 2: The sheet does not exist.选项 2:工作表不存在。 Using the template sheet as a template for the new sheet, all the information will be copied over, with the date in A2 as the name of this new sheet.使用模板工作表作为新工作表的模板,所有信息都将被复制过来,A2 中的日期作为新工作表的名称。 . . A2:D2 will be cleared from the template sheet as to leave a blank canvas. A2:D2 将从模板表中清除,留下空白 canvas。

I have tried to use a try catch statement, trying to find the sheet, and if that doesn't work, the catch creating the sheet with the new name.我尝试使用 try catch 语句,试图找到工作表,如果这不起作用,则使用新名称创建工作表的 catch。 I get the error "A sheet with the name "____" already exists."我收到错误“名称为“____”的工作表已存在。” though I am confused as to why, since I am receiving this error, it is not going on with the catch.虽然我很困惑为什么,因为我收到了这个错误,但它并没有继续下去。

I have also tried an if else statement with a similar idea, stating that if getSheetByName didn't work, it would create a sheet, or else it would set a new Active Sheet.我还尝试了一个具有类似想法的 if else 语句,指出如果 getSheetByName 不起作用,它将创建一个工作表,否则它将设置一个新的活动工作表。 This one has the same error.这个有同样的错误。

As for my code, I will show it, but I have to warn it's a bit chunky (for whatever reason, it won't acknowledge the variables within the try catch statement unless I restate them).至于我的代码,我会展示它,但我必须警告它有点笨重(无论出于何种原因,它不会承认 try catch 语句中的变量,除非我重述它们)。

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  ss.setActiveSheet(ss.getSheets()[0])
  var range = sheet.getRange("A2");
  var A2 = range.getValue();
  var templateSheet = ss.getActiveSheet();  
  var value = sheet.getRange("A2").getValue();
  var val = Utilities.formatDate(new Date(A2), "GMT+1", "MM/dd/yyyy");
  range.setValue(new Date(A2)).setNumberFormat("MM/dd/yyyy");
  A2 = SpreadsheetApp.getActiveSheet().getRange('A2').getValue();
   try {
        ss.setActiveSheet(ss.getSheetByName(A2));
        var sheet1 = ss.getSheetByName("GUI");
        var sheet2 = ss.getSheetByName(A2);
        sheet1.getRange("A2:D2").copyTo(sheet2.getRange(sheet2.getLastRow()+1,1,1,4), {contentsOnly:true});
        ss.setActiveSheet(ss.getSheets()[0]);
        sheet.getRange("A2:D2").clearContent();
    } catch (e) {
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheet = ss.getSheets()[0];
        var A2 = range.getValue();
        var templateSheet = ss.getActiveSheet();    
        var val = Utilities.formatDate(new Date(A2), "GMT+1", "MM/dd/yyyy");
        ss.insertSheet(val, ss.getSheets().length, {template: templateSheet});
        ss.setActiveSheet(ss.getSheets()[0]);
        sheet.getRange("A2:D2").clearContent();
    }
}

If anyone has any ideas why this isn't working, I'd love to know.如果有人有任何想法为什么这不起作用,我很想知道。 Thanks!谢谢!

The function getSheetByName returns null when it doesn't exist a sheet with that name.当 function getSheetByName不存在具有该名称的工作表时,它会返回null If you implement the code that way I think It will be easier.如果你以这种方式实现代码,我认为它会更容易。

Code would be something like this:代码将是这样的:

var sheet2 = ss.getSheetByName(A2);
if( ss.getSheetByName(A2) == null)
{
  //if returned null means the sheet doesn't exist, so create it
  var val = Utilities.formatDate(new Date(A2), "GMT+1", "MM/dd/yyyy");
  ss.insertSheet(val, ss.getSheets().length, {template: templateSheet});
  sheet2 = ss.insertSheet(A2);
}
//Rest of the behavior...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果值存在于工作表 1 和工作表 2 中,则复制行并将其粘贴到工作表 3。Google 表格。 Google Apps 脚本 - Copy row if value exists in both sheet 1 and sheet 2 and paste it to sheet 3. Google Sheets. Google Apps Script 使用 Google Apps 脚本,如何替换 Google 表格模板中的文本以制作新表格? - Using Google Apps Script, how can I replace text in a Google Sheets template to make a new Sheet? Google Apps脚本中用于从Google表格发送电子邮件的if条件不起作用 - The if condition in Google Apps Script to send an email from Google sheets doesn't work Google Apps Script - 如何将此代码应用于 Google 表格中的多个表格 - Google Apps Script - how to applies this code for multiple sheets in google sheets 如何将 Google Apps 脚本应用于多个工作表 - How to apply Google Apps Script to multiple sheets 谷歌表格谷歌应用程序脚本为所有行添加 VLOOKUP 函数 - Google Sheets google apps script add a VLOOKUP function for all rows 如何在 Google Sheets/Apps Script 中创建多选下拉复选框? - How to create multiple select drop-down check-box in Google Sheets/Apps Script? 如何在 Google Apps Script - JavaScript 中检查工作表名称/字符串是否同时包含字母和数字 - How to check if a sheet name / string contains both letters and numbers in Google Apps Script - JavaScript 如何在使用 Bootstrap 和 HTML5 从 Google Sheet 填充数据的 Google Sheet Apps 脚本中添加“数据列表” - How to Add 'Data List' in Google Sheet Apps Script that Populates Data from from Google Sheet with Bootstrap and HTML5 谷歌表格 - 宏不会将图表的格式复制到另一个表格 - Google Sheets - Macro doesn´t copy the format of a Chart to another Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM