简体   繁体   English

Google Sheets + Apps Script:如何根据单元格的值按名称删除工作表

[英]Google Sheets + Apps Script: How to delete a sheet by name based on the value of a cell

Hi I'm using this code to delete the row if the value in column "A" is older then 30 day and it's working good.嗨,如果“A”列中的值早于 30 天并且运行良好,我将使用此代码删除该行。 What I'm looking for is to also be able to get the value from column "B" and delete the sheet with the matching name before deleting the row.我正在寻找的是还能够从“B”列中获取值并在删除行之前删除具有匹配名称的工作表。

Thanks谢谢

function deleteOldRow() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Submited List");
  var datarange = sheet.getDataRange();
  var lastrow = datarange.getLastRow();
  var values = datarange.getValues();
  var currentDate = new Date();//today
  var monthOld = Date.now() - 30*24*3600*1000; 
  for (i=lastrow;i>=2;i--) {
var tempDate = values[i-1][0];
values[0] 
if ((tempDate!=NaN) && (tempDate <= (monthOld)))
{
  Logger.log(i)
  sheet.deleteRow(i);
   }
  }

}

在此处输入图片说明

I believe your goal as follows.我相信你的目标如下。

  • You want to delete the sheet by retrieving the sheet name from the column "B" when the row is deleted.您想通过在删除行时从“B”列中检索工作表名称来删除工作表。

If the value of column "B" is the sheet name, how about adding the following script to your script?如果“B”列的值是工作表名称,那么将以下脚本添加到您的脚本中如何?

Modified script:修改后的脚本:

if ((tempDate!=NaN) && (tempDate <= (monthOld))) {
  sheet.deleteRow(i);
  var deleteSheet = ss.getSheetByName(values[i-1][1]); // Added. Or ss.getSheetByName(values[i-1][1].trim());
  if (deleteSheet) ss.deleteSheet(deleteSheet); // Added
}
  • In this modification, when the sheet of sheet name values[i-1][1] is existing, the sheet is deleted.在该修改中,当存在工作表名称values[i-1][1]的工作表时,删除该工作表。

Reference:参考:

暂无
暂无

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

相关问题 如何使用 Google Apps 脚本根据单元格值获取 google 工作表名称? - How do I get a google sheet name based on cell value using Google Apps Script? 如何使用 Google Apps 脚本根据名称标准删除工作表? - How to delete sheets based on a name criteria using Google Apps Script? 如何在 Google Apps 脚本中使用单元格值作为工作表名称 - How to use a cell value as a sheet name in Google Apps Script Google表格根据当前标签的单元格值按名称调用工作表 - Google Sheets call sheet by name based on cell value of current tab 如何通过字符串值选择 Google 表格单元格并在 Google Apps 脚本中将其删除? - How do I select a Google Sheets cell by its string value and delete it in Google Apps Script? 如何更新 Google 表格中特定行的单元格(基于 Json 数据)使用 google apps 脚本创建新的 Jira 问题和更新表格 - How to update cell of a particular row in Google Sheets (based on Json data) using google apps script to create a New Jira issue and update sheet 根据 Google 表格单元格中的时间值删除行的脚本 - Script to delete rows based on a time value in a cell in Google Sheets 如何根据google工作表中的单元格值切换到可变工作表 - how to switch to a variable sheet based on a cell value in google sheets Google Apps 脚本如何按名称删除特定工作表 - How can Google Apps Script to delete a specific sheet by name 如何使用应用程序脚本在谷歌表格的特定单元格中设置值? - How to set a value in a particular cell in google sheets using apps script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM