Delete rows where checkbox is true and delete sheet where name matches value in checked row

huangapple go评论64阅读模式
英文:

Delete rows where checkbox is true and delete sheet where name matches value in checked row

问题

我正在尝试编写一个脚本,该脚本检查行并删除复选框被选中的行。我卡住的部分是,我有一个脚本,它还会创建一个新工作表并将其命名为这些行中一个单元格的值。我希望能够删除与被删除行中数值相匹配的工作表。

删除行的函数示例代码如下:

function deleteOldData() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getSheetByName("INCOMING");
var ui = SpreadsheetApp.getUi();

const receivedCol = ws.getRange("H:H").getValues()

var buttonPressed = ui.alert("警告:以下操作是不可逆的,您仍然要执行吗?",ui.ButtonSet.YES_NO);

if(buttonPressed == ui.Button.YES){
for(let i = receivedCol.length-1;i >= 0;i--){
if(receivedCol[i][0] === true) {
我想要检查被选中为true的行中单元格的值,存储这些值并删除名称与这些值相等的工作表
ws.deleteRow(i+1)
}
}
}
}

英文:

I'm attempting to write a script that checks rows and deletes rows where a checkbox is checked. The part where I'm stuck is that I have a script that also creates a new sheet and names it the the value of a cell in these rows. I'd like to be able to delete the sheet that matches the value in the row that gets deleted.

The example code for the function that deletes the row.

function deleteOldData() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName("INCOMING");
  var ui = SpreadsheetApp.getUi();

  const receivedCol = ws.getRange("H:H").getValues()

  var buttonPressed = ui.alert("WARNING: The following action is unrecoverable would you still like to execute?",ui.ButtonSet.YES_NO);

  if(buttonPressed == ui.Button.YES){
    for(let i = receivedCol.length-1;i >= 0;i--){
    if(receivedCol[i][0] === true) {
      ***I want to check the values in a cell of rows checked true, store the values and delete the sheets where name is equal to the values***
      ws.deleteRow(i+1)
    }
    } 
  }
}

I was able to delete sheets named after a specific cell but not able to delete every sheet for every row that's checked.

答案1

得分: 0

我通过对该函数进行一些调试,成功回答了自己的问题。

下面的代码按预期执行:

function deleteOldData() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getSheetByName("INCOMING");
var ui = SpreadsheetApp.getUi();

const receivedCol = ws.getRange("H:H").getValues()

var buttonPressed = ui.alert("警告:以下操作不可逆,您是否仍要执行?", ui.ButtonSet.YES_NO);

if(buttonPressed == ui.Button.YES){
for(let i = receivedCol.length-1;i >= 0;i--){
if(receivedCol[i][0] === true) {
let orderNum = ws.getSheetValues(i+1,3,1,1)
let orderSheet = ss.getSheetByName(orderNum);
ss.deleteSheet(orderSheet);
ws.deleteRow(i+1)
}
}
}
}


<details>
<summary>英文:</summary>

I was able to answer my own question with some fiddling with the function.

The code below executes as intended:

function deleteOldData() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getSheetByName("INCOMING");
var ui = SpreadsheetApp.getUi();

const receivedCol = ws.getRange("H:H").getValues()

var buttonPressed = ui.alert("WARNING: The following action is unrecoverable would you still like to execute?",ui.ButtonSet.YES_NO);

if(buttonPressed == ui.Button.YES){
for(let i = receivedCol.length-1;i >= 0;i--){
if(receivedCol[i][0] === true) {
let orderNum = ws.getSheetValues(i+1,3,1,1)
let orderSheet = ss.getSheetByName(orderNum);
ss.deleteSheet(orderSheet);
ws.deleteRow(i+1)
}
}
}
}


</details>



huangapple
  • 本文由 发表于 2023年3月31日 20:35:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定