如何将导入的值视为数组?

时间:2017-06-29 19:30:06

标签: javascript google-apps-script

我试图生成一个谷歌表单,下拉列表中有几百个选项。

我在单个单元格中拥有所有名称值,格式如下:

'用户1''用户2''用户3'

在代码中设置如下:

import tkinter as tk

WIDTH = {"MainMenu": 600, "Editor": 450}
HEIGHT = {"MainMenu": 500, "Editor": 501}

class ScreenMainMenu(tk.Frame):
  def __init__(self, parent, *args, **kwargs):
    tk.Frame.__init__(self, parent, *args, **kwargs)
    parent.parent.geometry("{}x{}".format(WIDTH["MainMenu"], HEIGHT["MainMenu"]))
    parent.config(width=WIDTH["MainMenu"], height=HEIGHT["MainMenu"])
    self.config(width=WIDTH["MainMenu"], height=HEIGHT["MainMenu"], background="red")
    self.grid()
    self.create_widgets()

  def create_widgets(self):
  #  self.logo = tk.Label(self, image=r"res\logo.png")
    self.logo = tk.Label(self, text="Build The Galaxy")
    self.button_new_game = tk.Button(self, text="New Game")
    self.button_load_game = tk.Button(self, text="Load Game")
    self.button_editor = tk.Button(self, text="Editor")
    self.button_exit = tk.Button(self, text="Exit")

    self.logo.grid(sticky="EW")
    self.button_new_game.grid(row=1, sticky="EW")
    self.button_load_game.grid(row=2, sticky="EW")
    self.button_editor.grid(row=3, sticky="EW")
    self.button_exit.grid(row=4, sticky="EW")

class ScreenEditor(tk.Frame):
  def __init__(self, parent, *args, **kwargs):
    tk.Frame.__init__(self, parent, *args, **kwargs)
    self.grid()
    parent.parent.geometry("{}x{}".format(WIDTH["Editor"], HEIGHT["Editor"]))

class MainApplication(tk.Frame):
  def __init__(self, parent, *args, **kwargs):
    tk.Frame.__init__(self, parent, *args, **kwargs)
    self.parent = parent
    self.grid()
    self.screen_open_main_menu()

  def screen_open_main_menu(self):
    self.screen_mainmenu = ScreenMainMenu(self)

  def screen_open_editor(self):
    self.screen_editor = ScreenEditor(self)

if __name__ == "__main__":
  root = tk.Tk()
  root.resizable(False, False)
  root.title("Build The Galaxy")
  main_app = MainApplication(root)
  root.mainloop()

当我使用如下所示的变量时,它将所有变量视为单值而不是数组。

var studentNames = SpreadsheetApp.openById('REDACTED').getSheetByName('Student List').getRange(3,3).getValues();

有什么帮助可以从这里开始吗?

3 个答案:

答案 0 :(得分:0)

是一串带单引号和逗号分隔的单词吗?如果是这样,你可以对逗号进行拆分 var s =数据 var arr = s.split(“,”); 现在你将拥有一个字符串数组。不确定这是否回答了你的问题。

答案 1 :(得分:0)

感谢大家让我指出了正确的方向。

原来分裂只是答案的一部分,我不得不先把它变成一个字符串。

的ToString()分割( “”);

答案 2 :(得分:0)

getValues()返回一个对象,因此您需要与它进行交互以获取数组的值字符串。

鉴于您只选择一个单元格,因此不需要遍历该对象,请尝试以下方法:

var studentNamesObj = SpreadsheetApp.openById('REDACTED').getSheetByName('Student List').getRange(3,3).getValues();
var studentNames = studentNamesObj[0][0].split(",");