【发布时间】:2021-12-13 22:20:46
【问题描述】:
我不确定要使用的正确术语,但我需要在 Google 表格中添加一行,以“阻止”PHP API v.4 从表格中获取更多数据。
我有一张表格,其中包含通过 API 获取的数百行数据,在我想要的行下方是一些我不想要的公式字段(我无法更改它们在表格中的位置或删除它们。)
如何在一行中的 A 单元格中添加类似“99999”的内容,以告知 API 在其前一行停止?
或者有没有更好的方法来告诉 API 停止?
这就是我在 Sheets v.4 PHP API 中用来从工作表中获取行的方法。数据由\n换行符分隔。
// authentication not shown
$range = 'Sheet3!AD2:Z';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
// This explode strings into array
foreach($values as $val) {
$valuearray = explode("\n", implode($val));
// This assigns variables that are used to echo the data
$firstname = $valuearray[0];
$lastname = $valuearray[1];
$address = $valuearray[2];
$city = $valuearray[3];
$state = $valuearray[4];
$zip = $valuearray[5];
这给了我这样的输出:
Joe\nSmith\n123 Dogpatch Lane\nDog Town\n阿拉巴马州\n34567
John\nJones\n456 Cat Hollow\nCat Town\nArkansas\n12345
不断……
我需要做的是在 A 单元格中添加一行内容(例如 999999),以停止前一行的 API。
21 年 12 月 15 日更新--------
我一定是想多了;这可行并且尽可能简单:
// This explode strings into array
foreach($values as $val) {
$valuearray = explode("\n", implode($val));
// This assigns variables that are used to echo the data
$firstname = $valuearray[0];
$lastname = $valuearray[1];
$address = $valuearray[2];
$city = $valuearray[3];
$state = $valuearray[4];
$zip = $valuearray[5];
if($firstname==='999999') { break; } // Break on 999999 in the firstname field
echo '<h1 class="firstname">' . $firstname . '</h1>';
......
【问题讨论】:
-
感谢您的回复。不幸的是,我无法复制
I still get those errors。当我测试我提出的脚本时,没有发生错误。这是因为我的技术不好。我对此深表歉意。对于This all may have to do with possibly a not good way that I explode/implode the arrays and assign variables.,我深表歉意,我提出的答案没有用。从此,我必须删除我的答案。因为我不想混淆其他用户。我为我糟糕的技术深表歉意。如果您能原谅我拙劣的技能,我将不胜感激。我认为我必须越来越多地学习。 -
您在寻找
if($firstname==='999999') { break; }吗? -
@Tanaike 你显然非常熟练,拥有 118K 代表! :) 我正在尝试做一些非常不寻常的事情,并且可能无法以干净的方式进行。我感谢您的帮助!我为你提供了帮助。谢谢!
-
感谢您的回复。为了确认您当前的问题,我可以再次询问您我提出的脚本吗?在我提议的脚本中,
I get Warning: Undefined array key 0 and Undefined array key 2041 on the line if (strval($v[$i][0]) == "99999") and also Undefined array key 1, etc. right after the 99999 from the sheet.的错误发生在哪里? -
我检查了@Tanaike 的答案,您共享的电子表格对我有用。你能确认这也对你有用吗?
标签: php google-sheets google-sheets-api