0

我在一个项目中,我被困在最后一步。

让我解释一下:我的项目过滤数据并将过滤后的数据移动到另一个电子表格。一切正常,没有问题,但发生了一些事情,问题是我需要输入动态数据作为过滤器和工作表名称。我创建了 2 个变量location,它们将确定过滤器并sectionSelect确定工作表名称。

我的目标是通过网络发送数据,并在所需的工作表中过滤其标签。

仅供参考:应用程序脚本绑定到 gsheet。

这是代码:

function doGet(e) {
  var ss = SpreadsheetApp.openByUrl("sheetURL")
  var sheet = ss.getSheetByName(Location);

  return TagData(e,sheet);
}

function doPost(e) {
  var ss = SpreadsheetApp.openByUrl("sheetURL")
  var sheet = ss.getSheetByName(Location);

  return TagData(e,sheet);
}

function TagData(e) {
  var Location = e.parameter.Location; // send from app with respective tag
  var sectiontSelect = e.parameter.sectiontSelect; // send from app with respective tag

  sheet.append([Location, sectiontSelect])
}

function FilterOnText() { 
  var ss = SpreadsheetApp.getActive()
  var range = ss.getDataRange();
  var filter = range.getFilter() || range.createFilter()
  var text = SpreadsheetApp.newFilterCriteria().whenTextContains(Location); // the location will be as per the location variable in the TagData function
  filter.setColumnFilterCriteria(1, text);
}

function titleAsDate() { 
  const currentDate = Utilities.formatDate(new Date(), "GMT+4", "dd-MM-yyyy HH:mm:ss");
  return SpreadsheetApp.create("Report of the " + currentDate);
}

function copyWithValues() {
  const spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
  const sourceSheet = spreadSheet.getSheetByName(sectionSelect); // fromApp variable will define which sheet the data will be copied (situated in the TagData function)
  const temp_sheet = spreadSheet.insertSheet('temp_sheet');
  const sourceRange = sourceSheet.getFilter().getRange();
  sourceRange.copyTo(
    temp_sheet.getRange('A1'),
    SpreadsheetApp.CopyPasteType.PASTE_NORMAL,
    false);
  SpreadsheetApp.flush();
  
  const sourceValues = temp_sheet.getDataRange().getValues();
  
  const targetSpreadsheet = titleAsDate();
  
  const rowCount = sourceValues.length;
  const columnCount = sourceValues[0].length;
  
  const targetSheet = targetSpreadsheet.getSheetByName('Sheet1').setName("Report"); // renamed sheet
  const targetRange = targetSheet.getRange(1, 1, rowCount, columnCount);
  targetRange.setValues(sourceValues);
  spreadSheet.deleteSheet(temp_sheet);
} 

function MoveFiles(){ 
  var files = DriveApp.getRootFolder().getFiles();
  var file = files.next();
  var destination = DriveApp.getFolderById("1wan7PLhl4UFEoznmsN_BVa2y4AtFaCOr");
  destination.addFile(file)
  var pull = DriveApp.getRootFolder();
  pull.removeFile(file);
}

function clearFilter() { // clearance of filters applied in first function
 var ss = SpreadsheetApp.getActiveSpreadsheet(); 
 var sheet = ss.getSheetByName("testo"); 
 sheet.getFilter().remove(); 
}
4

1 回答 1

0

解释:

两个问题:

  1. TagData不返回任何东西(正如库珀指出的那样)

  2. TagData(e)只有一个参数,但是从and函数调用它时e传递了两个参数。TagData(e,sheet)doGetdoPost

不确定这是否会解决您的所有问题,但它会解决我上面提到的问题。

解决方案:

  1. 从问题 1) 中删除这些return陈述。doGetdoPost

  2. 在函数中添加sheet参数TagData

结果代码:

function doGet(e) {
  var ss = SpreadsheetApp.openByUrl("sheetURL")
  var sheet = ss.getSheetByName(Location);
  TagData(e,sheet); // modified
}

function doPost(e) {
  var ss = SpreadsheetApp.openByUrl("sheetURL")
  var sheet = ss.getSheetByName(Location);
  TagData(e,sheet); // modified
}

// added the sheet parameter
function TagData(e,sheet) {
  var Location = e.parameter.Location; // send from app with respective tag
  var sectiontSelect = e.parameter.sectiontSelect; // send from app with respective tag   
  sheet.append([Location, sectiontSelect]);
}

// rest of your code..

或者试试这个:

function doGet(e) {
  var ss = SpreadsheetApp.openByUrl("sheetURL")
  var sheet = ss.getSheetByName(Location);
  var params = JSON.stringify(TagData(e,sheet)); // modified
  return HtmlService.createHtmlOutput(params); // added
}

function doPost(e) {
  var ss = SpreadsheetApp.openByUrl("sheetURL")
  var sheet = ss.getSheetByName(Location);
  ContentService.createTextOutput(JSON.stringify(e))
}

// added the sheet parameter
function TagData(e,sheet) {
  var Location = e.parameter.Location; // send from app with respective tag
  var sectiontSelect = e.parameter.sectiontSelect; // send from app with respective tag   
  sheet.append([Location, sectiontSelect]);
  return { Location: Location, sectiontSelect: sectiontSelect }
  
}
于 2021-02-22T10:49:22.583 回答