简体   繁体   English

Google电子表格中的Javascript

[英]Javascript in Google Spreadsheet

I'm trying to write a simple script for a Google Docs Spreadsheet. 我正在尝试为Google文档电子表格编写一个简单的脚本。 The two links below are a screenshot of the sheets I'm writing the script for. 以下两个链接是我正在为其编写脚本的工作表的屏幕截图。

I'm still learning how to write JavaScript but this is what I'm trying to do. 我仍在学习如何编写JavaScript,但这就是我想要做的。 I hope this makes sense. 我希望这是有道理的。 Can anyone help me with this? 谁能帮我这个?

get.sheetbyname "Purchase Orders"
    If column E  == "stock"
    var qty == three cells left of cell e
    var partNum == two cells left of cell e

get.sheetbyname "Parts inventory"
    loop through column C until you find partNum
    var oldQty == number(two cells left of partNum)
    add qty to oldQty to get new qty

You'll definitely need to get familiar with JavaScript quickly. 您肯定需要快速熟悉JavaScript。 It's a powerful language and seems easy to pick up but has many differences from other popular, seemingly related languages (eg Java, C, etc). 它是一种功能强大的语言,似乎很容易上手,但是与其他流行的,看似相关的语言(例如Java,C等)有很多差异。 Once you're comfortable with JavaScript, you'll want to familiarize yourself with the Google Apps Script Spreadsheet Services APIs . 熟悉JavaScript后,您需要熟悉Google Apps脚本电子表格服务API These APIs allow you to script Google Docs spreadsheets like you would other spreadsheet applications, such as Microsoft Excel. 通过这些API,您可以像其他电子表格应用程序(例如Microsoft Excel)一样为Google Docs电子表格编写脚本。 Start by opening your workbook and browsing to "Tools > Scripts > Script Editor...", then creating a new named script. 首先打开工作簿并浏览到“工具>脚本>脚本编辑器...”,然后创建一个新的命名脚本。

Here's a start at what you're trying to do specifically. 这是您要尝试做的一个开始。 It looks like you're trying to find out how many purchase orders exist for some item and update the quantity in the inventory sheet. 看来您正在尝试找出某个项目存在多少采购订单并更新库存表中的数量。

function updateInventoryFromPurchaseOrders() {
  var purchaseOrders = {}; // A mapping of partNumber => quantity.
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Purchase Orders');
  if (sheet) {
    // For each row, if column "E" == "stock" then set partNumber, quantity.
    purchaseOrders[partNumber] = quantity;
  }
  // Now purchaseOrders should look like {'SL249':5, 'ML50':1, 'MWF':1}

  sheet = ss.getSheetByName('Inventory');
  if (sheet) {
    // For each row, set quantity, partNumber.
    var purchased = purchaseOrders[partNumber];
    // Set column "A" value = quantity + purchased
  }
}

Reading and writing the values from individual cells will require you to use Ranges , the docs linked above should give you enough examples to get you the rest of the way. 读取和写入单个单元格中的值将需要您使用Ranges ,上面链接的docs应该为您提供足够的示例,以帮助您了解其余的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM