如何在Google表格API中写入表格的特定范围 [英] How to write to a specific range of a sheet in google sheets API

查看:142
本文介绍了如何在Google表格API中写入表格的特定范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用Google Sheets API,我正在尝试编写一个可以接收一些数据并对其进行处理的脚本,该脚本可以正常工作,然后我想将已处理的数据写入一个已经存在的列中现有工作表,这是我需要帮助的.

This is my first time using the google sheets API, and I'm trying to write a script that takes in some data and processes it, which works, and then I want to write the processed data to a column in an already existing sheet, which it what I need help with.

是否有更好的方法将数组放置在特定范围内?

Is there some better way to put an array in a specific range?

错误消息说无法获取null范围,这是我的代码,到目前为止,不属于代码块的那一行是问题所在:

The error message says cannot get range of null, this is my code so far, the line that isn't part of the block is the issue:

function getList() {
 var spreadsheetId = '1BjnZ-SPBVcCdITxcvEX1nCPecvGdYSKQ0m_GiaKduZQ';
 var rangeName = 'Paste Here!I1:GZ6';
 var pasteRange = 'The Goods!B3:B1000';
 var values = Sheets.Spreadsheets.Values.get(spreadsheetId, rangeName).values;
 var width = values[0].length;
 var teamNumbers = [];
 if (!values) {
   Logger.log('No data found.');
 } else {
   for(var col = 1; col < width; col+=2) {
     for(var row = 0; row < values.length; row++) {
       teamNumbers += values[row][col].toString();
       teamNumbers += '\n'
     }
    }

SpreadsheetApp.getActiveSpreadsheet().getRange(pasteRange).setValues(teamNumbers);

SpreadsheetApp.getActiveSpreadsheet().getRange(pasteRange).setValues(teamNumbers);

  }
}

还有第二个问题,如何才能使某个对象的范围成为变量,例如为了从参数中获取I1:GZ6?

Also, secondary question, how can you make the range for something a variable, as in in order to get I1:GZ6 from a parameter?

非常感谢您的帮助!

推荐答案

出现错误的唯一原因

无法调用null的方法getRange

如果您使用的是独立脚本而不是绑定脚本,则从SpreadsheetApp.getActiveSpreadsheet()行中的

表示.独立脚本必须使用SpreadsheetApp.openById("some id")才能访问电子表格.

from the line SpreadsheetApp.getActiveSpreadsheet() is if you are using a standalone script instead of a bound script. Standalone scripts must use SpreadsheetApp.openById("some id") in order to access a spreadsheet.

有关差异的更多信息,请参阅Apps脚本文档

For more information on the differences, consult the Apps Script documentation

独立脚本
SpreadsheetApp#getActiveSpreadsheet

这篇关于如何在Google表格API中写入表格的特定范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆