gpt4 book ai didi

typescript - Google 脚本删除按钮无法正常运行

转载 作者:行者123 更新时间:2023-12-05 04:22:00 25 4
gpt4 key购买 nike

**我正在尝试从 google 表格中删除该行,并且我已将脚本分配给此按钮,但当我单击删除按钮时它没有删除记录。它给出了消息 Reacod not found。我还制作了一个正常运行的搜索按钮 **

函数 deleteRow() {

var myGooglSheet= SpreadsheetApp.getActiveSpreadsheet(); //declare a variable and set with active google sheet

var shUserForm= myGooglSheet.getSheetByName("User Form"); //delcare a variable and set with the User Form worksheet

var datasheet = myGooglSheet.getSheetByName("SUPPORTING SHEET"); ////delcare a variable and set with the Database worksheet

//to create the instance of the user-interface environment to use the message box features var ui = SpreadsheetApp.getUi();

// Display a dialog box with a title, message, and "Yes" and "No" buttons. The user can also // close the dialog by clicking the close button in its title bar.

var response = ui.alert("Delete", 'Do you want to delete the record?',ui.ButtonSet.YES_NO);

var str = shUserForm.getRange("E13").getValue();

var values = datasheet.getDataRange().getValues(); //getting the entire values from the used

range and assigning it to values variable

var valuesFound=false; //variable to store boolean value to validate whether values found or not

for (var i = 0; i<values.length; i++) { var rowValue = values&[i]; //declaraing a variable and storing the value

//checking the first value of the record is equal to the search item
if (rowValue[0]==str) {

var iRow = i+1;
datasheet.deleteRow(iRow);

ui.alert("Record Deleted!");

shUserForm.getRange("E13").clear();
shUserForm.getRange("E15").clear();
shUserForm.getRange("E17").clear();
shUserForm.getRange("E19").clear();
shUserForm.getRange("E21").clear();
shUserForm.getRange("E23").clear();
shUserForm.getRange("E27").clear();
shUserForm.getRange("E29").clear();
shUserForm.getRange("E31").clear();
shUserForm.getRange("E37").clear();
shUserForm.getRange("E39").clear();
shUserForm.getRange("E41").clear();
shUserForm.getRange("E43").clear();
shUserForm.getRange("E45").clear();
shUserForm.getRange("E49").clear();
shUserForm.getRange("E51").clear();
shUserForm.getRange("E53").clear();

valuesFound=true;

return;
}
}

if(valuesFound==false)
{
ui.alert("No RECORD FOUND!");

}
}

最佳答案

修改点:

  • 如果 未找到 Reacod,它会给出消息 Reacod not found。ui.alert("No RECORD FOUND!"); 并且如果 var str = shUserForm.getRange("E13").getValue() 的值正确并且“SUPPORTING SHEET”表的“A”列的值为 str,我认为您出现问题的原因是 var rowValue = values&[i];,如 TheWizEd's comment 所述.但是从我已经尝试通过删除“&”但仍然无法正常工作,我担心该列可能与您预期的列不同。

    • 在这种情况下,请再次检查 var str = shUserForm.getRange("E13").getValue() 的值。
  • 顺便说一下,在您的脚本中,ui 没有声明。请注意这一点。

  • 在您的脚本中,responsevar response = ui.alert("Delete", 'Do you want to delete the record?',ui.ButtonSet.YES_NO) ; 未使用。在这种情况下,此行之后的脚本会针对"is"和“否”运行。

  • 在您的脚本中,我认为可以从您的显示脚本中减少一些过程成本。

当这些点都体现在你的脚本中,下面的修改怎么样?

修改后的脚本:

function deleteRow() {
var column = "A"; // Please set the column letter you want to search.

var ui = SpreadsheetApp.getUi();
var myGooglSheet = SpreadsheetApp.getActiveSpreadsheet();
var shUserForm = myGooglSheet.getSheetByName("User Form");
var datasheet = myGooglSheet.getSheetByName("SUPPORTING SHEET");
var response = ui.alert("Delete", 'Do you want to delete the record?', ui.ButtonSet.YES_NO);
if (response == ui.Button.NO) return;
var str = shUserForm.getRange("E13").getValue();
var ranges = datasheet.getRange(`${column}1:${column}${datasheet.getLastRow()}`).createTextFinder(str).matchEntireCell(true).findAll();
if (ranges.length == 0) {
ui.alert("No RECORD FOUND!");
return
};
ranges.reverse().forEach(r => datasheet.deleteRow(r.getRow()));
shUserForm.getRangeList(["E13", "E15", "E17", "E19", "E21", "E23", "E27", "E29", "E31", "E37", "E39", "E41", "E43", "E45", "E49", "E51", "E53"]).clear(); // or clearContent()
SpreadsheetApp.flush();
ui.alert("Record Deleted!");
}
  • 运行此脚本时,会从“用户表单”工作表的“E13”中检索搜索值。并且,使用搜索值,使用 TextFinder 搜索行。在这种情况下,脚本可以使用 var column = "A"; 设置搜索列。找到搜索值后,删除该行,范围 ["E13", "E15", "E17", "E19", "E21", "E23", "E27", "E29", “E31”、“E37”、“E39”、“E41”、“E43”、“E45”、“E49”、“E51”、“E53”] 被清除。

  • 如果要更改搜索列,请修改var column = "A";

引用资料:

关于typescript - Google 脚本删除按钮无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74101205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com