Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 将新数据添加到工作表中而不在GAS中覆盖_Google Apps Script - Fatal编程技术网

Google apps script 将新数据添加到工作表中而不在GAS中覆盖

Google apps script 将新数据添加到工作表中而不在GAS中覆盖,google-apps-script,Google Apps Script,我需要找出要添加到脚本中的内容,以便在不覆盖现有数据的情况下将新数据添加到工作表的末尾。我是一个完全的初学者,已经对此进行了研究,但似乎无法将代码组合在一起。这是我的剧本: function myFunction() { var url = "xxxxxxxx" var response = UrlFetchApp.fetch(url) var data = response.getContentText() var result =

我需要找出要添加到脚本中的内容,以便在不覆盖现有数据的情况下将新数据添加到工作表的末尾。我是一个完全的初学者,已经对此进行了研究,但似乎无法将代码组合在一起。这是我的剧本:

   function myFunction() {
  var url = "xxxxxxxx"
  
  var response = UrlFetchApp.fetch(url)
  
  var data = response.getContentText()
  
  var result = JSON.parse(data)
  
  var sheet = SpreadsheetApp.getActiveSheet()
  
  sheet.clear()
  
  var headerRow = ['Title','Brand','UPC','In Stock','Stock Level','Price','Seller','Next Day','Ship to Home', 'Link']
  
  sheet.appendRow(headerRow)
  
  for(var i=0; i<result.category_results.length; i++){
    
    var row = [result.category_results[i].product.title, result.category_results[i].product.brand, result.category_results[i].product.upc,result.category_results[i].inventory.in_stock, 
               result.category_results[i].inventory.stock_level, result.category_results[i].offers.primary.price, result.category_results[i].offers.primary.seller_name, 
                 result.category_results[i].fulfillment.next_day_shipping_eligible, result.category_results[i].fulfillment.ship_to_home, result.category_results[i].product.link]
    
    sheet.appendRow(row)
  
  }

}
函数myFunction(){
var url=“xxxxxxxx”
var response=UrlFetchApp.fetch(url)
var data=response.getContentText()
var result=JSON.parse(数据)
var sheet=SpreadsheetApp.getActiveSheet()
表1.clear()
var headerRow=[‘标题’、‘品牌’、‘UPC’、‘库存’、‘库存水平’、‘价格’、‘卖家’、‘次日’、‘送货上门’、‘链接’]
表.附录行(标题行)
对于(变量i=0;修改点:
  • 在您的脚本中,每次运行
    sheet.clear()
    ,都会清除工作表。这样,现有值总是会被删除,工作表会被新值覆盖。我认为这就是您出现问题的原因
  • 在您的脚本中,
    appendRow
    用于循环。在这种情况下,处理成本将很高
  • 为了将新值附加到工作表中,我想提出以下流程。
  • 检索值。(这是您的脚本。)
  • 检查图纸中是否存在现有值
  • 创建用于将值放入工作表的数组
  • 使用数组放置值。
    • 在这种情况下,使用
      setValues
      代替
      appendRow
当上述各点反映到脚本中时,它将变成如下所示

修改脚本:
函数myFunction(){
//1.检索值。(这是您的脚本。)
var url=“xxxxxxxx”;
var response=UrlFetchApp.fetch(url);
var data=response.getContentText();
var result=JSON.parse(数据);
//2.检查表中是否存在现有值。
var sheet=SpreadsheetApp.getActiveSheet();
var lastRow=sheet.getLastRow();
如果(lastRow==0){
var headerRow=[‘标题’、‘品牌’、‘UPC’、‘库存’、‘库存水平’、‘价格’、‘卖家’、‘次日’、‘送货上门’、‘链接’];
表1.附录行(标题行);
}
//3.创建用于将值放入工作表的数组。
var值=[]

对于(var i=0;Itaaike,非常感谢!!这非常有帮助。我昨天刚刚开始学习编写代码!@Ben Turner感谢您的回复。我很高兴您的问题得到解决。
function myFunction() {
  // 1. Retrieve values. (This is your script.)
  var url = "xxxxxxxx";
  var response = UrlFetchApp.fetch(url);
  var data = response.getContentText();
  var result = JSON.parse(data);
  
  // 2. Check whether there are the existing values in the sheet.
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  if (lastRow == 0) {
    var headerRow = ['Title','Brand','UPC','In Stock','Stock Level','Price','Seller','Next Day','Ship to Home', 'Link'];
    sheet.appendRow(headerRow);
  }
  
  // 3. Create an array for putting the values to the sheet.
  var values = []
  for(var i=0; i<result.category_results.length; i++){
    var row = [result.category_results[i].product.title, result.category_results[i].product.brand, result.category_results[i].product.upc,result.category_results[i].inventory.in_stock,
    result.category_results[i].inventory.stock_level, result.category_results[i].offers.primary.price, result.category_results[i].offers.primary.seller_name,
    result.category_results[i].fulfillment.next_day_shipping_eligible, result.category_results[i].fulfillment.ship_to_home, result.category_results[i].product.link];
    values.push(row);
  }
  
  // 4. Put the values using the array.
  sheet.getRange(lastRow + 1, 1, values.length, values[0].length).setValues(values);
}