[英]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.