接受用户输入后使用应用程序脚本恢复脚本执行时出错

问题描述 投票:0回答:1

我正在运行一个应用程序脚本,它接受一个 Json 字符串,将其转换为一个对象,并将一些键值添加到 Google 工作表中。以下代码适用于某些 JSON 字符串。然而,对于其他人,我在标题中得到了错误。当我检查执行日志时,我看到:

建议接受JSON字符串后不重新启动。知道为什么吗?

function importJsonToSheet() {
  // Get the active spreadsheet
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  
  // Get the active sheet
  var sheet = spreadsheet.getActiveSheet();
  
  // Check if the active sheet is named "Sheet7"
  if (sheet.getName() === "sheet7") {
    // Prompt the user to input the JSON data
    var ui = SpreadsheetApp.getUi();
    var prompt = ui.prompt('Enter JSON data', 'Please paste your JSON data here:', ui.ButtonSet.OK_CANCEL);
    var response = prompt.getResponseText();
    var button = prompt.getSelectedButton();
    
    // Check if the user clicked "OK"
    if (button === ui.Button.OK) {
      // Parse the JSON string into an object
      var jsonObject;
      try {
        jsonObject = JSON.parse(response);
      } catch (e) {
        ui.alert("Invalid JSON format. Please enter valid JSON data.");
        return;
      }
      
        addRowFromObject(jsonObject.Single, sheet) ;


    } else {
      // If the user clicked "Cancel", display a message
      ui.alert("Operation canceled.");
    }
  } else {
    // If the active sheet is not named "Sheet7", display an alert
    SpreadsheetApp.getUi().alert("This script can only run on Sheet7. Please activate Sheet7.");
  }
}

编辑:

function addRowFromObject(myObj, sheet) {

  Logger.log('in addRowFromObject');
  Logger.log(myObj);



  var headerRow = sheet.getFrozenRows(); // THIS SHOULD GET HEADER ROW 
  var headings = sheet.getDataRange().offset(headerRow - 1, 0, 1).getValues()[0];
  var newRow = [];

  headings.forEach(function (heading) {

    if (myObj.hasOwnProperty(heading)) {
      newRow.push(myObj[heading])
    } else {
      newRow.push('');
    }


  });


  Logger.log(newRow);




  return newRow


}
javascript google-apps-script
1个回答
0
投票

推荐

我对您现有的代码做了一些更改,下面是我的建议,可以按预期工作:

function importJsonToSheet() {
  // Get the active spreadsheet
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  
  // Get the active sheet
  var sheet = spreadsheet.getActiveSheet();
  
  // Check if the active sheet is named "Sheet7"
  if (sheet.getName() === "sheet7") {
    // Prompt the user to input the JSON data
    var ui = SpreadsheetApp.getUi();
    var prompt = ui.prompt('Enter JSON data', 'Please paste your JSON data here:', ui.ButtonSet.OK_CANCEL);
    var response = prompt.getResponseText();
    var button = prompt.getSelectedButton();
    
    // Check if the user clicked "OK"
    if (button === ui.Button.OK) {
      // Parse the JSON string into an object
      var jsonObject;
      try {
        jsonObject = JSON.parse(response);
      } catch (e) {
        ui.alert("Invalid JSON format. Please enter valid JSON data.");
        return;
      }
      
        addRowFromObject(jsonObject, sheet) ;

    } else {
      // If the user clicked "Cancel", display a message
      ui.alert("Operation canceled.");
    }
  } else {
    // If the active sheet is not named "Sheet7", display an alert
    SpreadsheetApp.getUi().alert("This script can only run on Sheet7. Please activate Sheet7.");
  }
}

function addRowFromObject(myObj, sheet) {

  Logger.log('in addRowFromObject');
  Logger.log(myObj);

  var headerRow = sheet.getFrozenRows(); // THIS SHOULD GET HEADER ROW 
  var headings = sheet.getDataRange().offset(headerRow - 1, 0, 1).getValues()[0];
  var newRow = [];

  headings.forEach(function (heading) {

    if (myObj.hasOwnProperty(heading)) {
      newRow.push(myObj[heading])
    } else {
      newRow.push('');
    }
  });

  Logger.log(newRow);
  sheet.appendRow(newRow);

  return newRow

}

我从

.Single
函数中的这一行
addRowFromObject(jsonObject.Single, sheet) ;
中删除了
importJsonToSheet
,并在
sheet.appendRow(newRow);
函数中添加了这一行
addRowFromObject
,以将对象值添加到工作表中,这是我的示例工作表的输出:

参考

尝试一下,如果有效请告诉我!

© www.soinside.com 2019 - 2024. All rights reserved.