Skip to content

Instantly share code, notes, and snippets.

@aoxu
Created March 18, 2013 08:16
Show Gist options
  • Save aoxu/5185720 to your computer and use it in GitHub Desktop.
Save aoxu/5185720 to your computer and use it in GitHub Desktop.
用于在现金明细表中自动求余额。这是用于Google Drive里电子表格(Spreadsheet)的脚本,使用Google Apps Script编写,类似于JavaScript。
function sumBalance() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
for (var i = 2; i <= numRows; i++) {
var rowSum = 0;
for (var j = 2; j <= i; j++) {
rowSum = rowSum + sheet.getRange(j, 8, 1, 1).getValue();
}
sheet.getRange(i, 9, 1, 1).setValue(rowSum);
}
}
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Sum Balance",
functionName : "sumBalance"
}];
sheet.addMenu("Script Center Menu", entries);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment